pre-filled cells in new ipython notebook - ipython

When I make a new ipython notebook, I would like to have a pre-filled one.
By pre-filled, I mean that instead of a first blank cell, I would like in it a custom code like
import os
import sys
...
In other words, I do not want to type or copy/paste in every notebook the same piece of code I use 90% of my time.
I do not want this code to be executed behind the scene. It should appear in the notebook.

Related

jupyter notebook vscode doesn't display dataframe right

I used to always have my dataframes displayed correctly in a nice table in jupyter notebook but now everytime I display one, it's like I printed it unformated.
I tried to use display(df_main) but it's still not displaying it correctly.

jupyter notebook display cells horizontal

I have issues on jupyter notebook conversion. It looks fine on jupyter notebook. However if I convert it to html or upload to github, it displays like photo above.
Instead of vertical(scroll up and down), I have to scroll left and right and each cell are really narrowed.
Any suggestion?
Thanks in advance!
This is typical of a library you use emitting invalid HTML. I would suggest opening an issue or contacting the Jupyter mailing list to help you debug this as StackOverflow will not be the right place to have a back and forth.
You should be able to narrow down which library does that by:
- backup your notebook.
- Delete cells one and until notebook looks normal. (tip use nbconvert locally instead of uploading, and use python -m http.server)
- the last cell you delete have a issue.
It may be a bug in nbconvert but I doubt it.

keep cell metadata in a copy of a jupyter notebook?

I'm working on a jupyter notebook (from a local installation of jupyterhub) that I want to be able to copy and distribute to my coworkers. Some of them are a little 'code-phobic' so I want to be able to portions of the code from them.
I can use the hide input extension to very prettily hide the code in my own notebook, however when I make a copy the cell meta-data for hide input reverts to "hide_input": false, and the code is all automatically visible.
Is there a way to keep cell meta data in the copying process?
I can find a lot of online discussions/documentation about keeping hide input functionality with nbconvert, however I don't want to convert to html because I still need the users to be able to actually run code.
Is there a way to use nbconvert to make a copy of a notebook and not actually convert it?
Thanks.
I'm confused - because for me cell metadata is preserved when copied. Are you sure you are actually saving the notebook after you have changed the cell metadata?
Regardless, you can use nbconvert to "convert" a notebook into a notebook; use the command:
jupyter nbconvert --to notebook --execute mynotebook.ipynb
From the docs: https://nbconvert.readthedocs.io/en/latest/usage.html#notebook-and-preprocessors
If you use the Toggle selected cell input button or individually edit cell metadata the individual cell hide input status was not preserved. But when I used the Hide codecell inputs to hide the input of all the cells; then it was preserved after copying.

how to make a single slide with markdown and code in ipython notebook?

In this tutorial presentation they use this, but, apparently, they don't show you how to do it.
http://www.damian.oquanta.info/posts/make-your-slides-with-ipython.html
Thanks,
I am sorry, I completely misunderstood your question.
If you mean embedding code which you do not execute, then you can use simple markdown syntax (similar to StackOverflow for example). To include inline code, just but it between backticks:
For example `a[::2]` selects every second element of the list `a`.
If you want to include a code block, either indent the whole block by 4 spaces, or put the block between 3 backticks (the latter also supports syntax highlighting if you specify the language):
a = foo()
b = bar(a)
or
```python
a = foo()
b = foo(a)
```
For more, see this markdown tutorial.
If you want to include executable cells and markdown on the same slide, then you need to put your code and markdown into different cells, and for slide type select slide for the topmost cell, and - for the subsequent cells that you want to have on the same slide. - means "do not start a new slide for this cell".
Old answer (how to convert a notebook to a reveal.js presentation)
You can convert your ipython notebook to a Reveal.js presentation using nbconvert.
First you have to specify the structure of the presentation. For that click on View -> Cell Toolbar -> Slideshow. Then at the top of every cell you can select whether you want it to be a new slide (horizontal direction), a subslide (vertical direction), a fragment (appear on the same slide with a fade-in) and some others.
Once you are set, you can use the nbconvert utility to transform your notebook into a presentation. The basic usage is
jupyter nbconvert example.ipynb --to slides
Then you get a HTML which you can serve up later using some web server.
If you want to show off your slides right away, jupyter provides a shortcut: you can do
jupyter nbconvert example.ipynb --to slides --post serve
Using this jupyter starts a local server after the conversion and opens the presentation in your default browser.
Last, if you want to easily share your presentation online, you can upload the notebook to some online repository (for example to github as a gist), and you can use nbviewer to share it. It will have a box-like icon in the top right corner. If you press it, it will change from notebook mode to presentation mode (I don't understand the box analogy either).
For more info please consult the jupyter documentation.

IPython Tab Completion Broken

I am new to using IPython 4.0.0 and I am having a problem with tab completion.
At the moment, tab completion works for navigating through my directory structure, but it doesn't work for Python modules etc. If I import numpy and type:
numpy.[tab]
then nothing shows up.
So following the advice on this question: IPython tab completion not working, I installed readline and pyreadline, but it still didn't help. Then I added this code to my startup.py file:
try:
import readline
except ImportError:
print("Module readline not available.")
else:
import rlcompleter
readline.parse_and_bind("tab: complete")
Now when I type
numpy.[tab]
it shows me a list of attributes as I would expect. However, now I can't use tab complete to navigate around my directory structure, which is incredibly frustrating. It seems that IPython is meant to have this functionality built in, so can anyone suggest how I might get it working?
Cheers