Python Jupyter Notebook: Specify cell execution order - python

I have a Jupyter notebook.
In the cell 1, I defined a lot of functions, which need to run before other things. Then in the following cells, I start to present result.
However, when I convert to HTML, this layout is ugly. Readers have to scroll a long time to see the result and they may not care about the functions at all.
But I have to put the code in that order because I need those functions.
So my question is, is there a way I could control the run order of cells after I click run all? or is there a way I could do something like the following.
I put all my function definitions in cell 20, then in cell 1, I could say tell Jupyter something like "run cell 20".
Just curious if this is doable.
Thanks.

I would save the functions as a separate module, then import this module at the beginning.

Such a functionality, (to my knowledge) is not available in Jupyter as of yet. However, if you are really worried about having a lot of function definitions at the beginning and want to hide them, you can do the following alternative:
Define the functions in a Python script.
Add the script execution to the first coding cell of your notebook
Add the remaining of the code to the consecutive cells of the notebook
Optionally, show its contents at the end of the notebook for viewers' convenience.

Check out the execution_dependencies nbextension. With that, you can define dependencies on the order of the execution of your cells.
To use tags on your cells:
View - Cell Toolbar - Tags
E.g.:
Add tags to your cells
Cell 1 - #HTML, =>functions
print(txt)
...
Cell 20 - #functions
txt = 'functions'
When you run Cell 1 it will output 'functions' because it will run cell 20 first.

Consider using the Runtools nbextension, which allows you to run selected cells.

Related

Python in VSCode: Why can come cell outputs be set to HTML, while others only text?

I am using Jupyter notebooks and Python in VSCode. In the recent update (I don't know the version numbers), they put in the functionality to change the output style. However, some cell output allow me to set it to HTML, while others only text. Why would this be?
And it can't be the contents/code - for example, I have one cell which gives nice HTML output for a describe method:
While, further down, I have the same, but then I get
any ideas?
Also any idea as to why the two instances of the same method is not giving the same results?
It looks like your 2nd cell doesn't call the function describe, it simply accesses it.
However, if you ever run into an issue with displaying, for instance, pandas as raw text instead of an HTML table, you probably want to look into IPython's display module.
You can import it like so:
from IPython.display import display
display is actually run under-the-hood on your last line of code. This is why writing:
df
will show you a pd.DataFrame as a table, whereas that won't happen in a program or regular Python console.
There are also a host of other tools in IPython.display, like Markdown, HTML, and Image, to name a few.
That's because describe() and describe are different. Use function type(), you can see describe() is pandas.core.series.Series, while describe is method:
So the data they show is different too.

Is there any straightforward way to change part of copied text (with python code) into Markdown and Code cell in Jupyter notebook

I copied a long text from a webpage including several Python code sections and text sections into my Jypter notebook. Below what I copied and pasted.
Is there any straightforward way to convert the code sections to executable Code cell and leave the rest text as Markdown instead of inserting new cell one by one for the code sections? (codes and texts are in separated paragraphs). Below what I want:
Thank you for your time in advance.
If it is just a few lines as in your example, the manual way is most convenient. Paste everything in one cell, and then split the cell by using a keyboard shortcut for that (Ctr-Shift-Minus) and use another keyboard shortcut (M) to change the types of cells with comments in it to Markdown.
If you have lots of material to convert, Jupytext might help. https://github.com/mwouts/jupytext .

Execute Highlighted Code in Jupyter notebook Cell?

Is it possible to execute ONLY the highlighted code in a Jupyter notebook cell? This is possible in Spyder and RStudio.
I find this to be quite useful for trouble-shooting code as you write.
If a cell contains:
a=13
b=17
c=42
a=a*c
I'd like to be able to highlight and run only the desired lines (e.g. variable assignmemnts), but not the final line.
I use this frequently in Spyder and RStudio, would love to do in Jupyter as well. I find I am constantly splitting and re-combining cells in order to troubleshoot a single line of code, where for example, I've indexed into something incorrectly. Highlighting and printing the variable allows me to see what I've actually assigned it to be and is throwing an error, vs. what I had intended.
There is no such thing in Jupyter as 'highligh and run'. At least I am not aware of it.
Run the cell after commenting the other lines out using CTRL + /, split cells and execute only the chosen ones or use a debugger (e.g. pudb, it works in Jupyter) to change variables values on the fly (while debugging).
It seems now it is available in python notebook as well.
https://github.com/jupyterlab/jupyterlab/pull/2191
If I open a python notebook in Kaggle (www.kaggle.com) and select a text, it lets me run only the highlighted part.

Jupyter get arbitrary notebook cell contents

I would like to access the textual contents of another cell in the notebook from Python so that I can feed it to the unit testing script, regardless of whether it has been executed or not. It seems like this should be possible, but I can't find the right API to do it in the IPython kernel. Thoughts?
This question asks the same question, but I don't really want to use magics unless I have to. Ideally the workflow would be "select widget choice" followed by "click widget" to cause the tests to run.
Background
I'm working with some students to teach them Python, and in the past when I've done this I've set up a bunch of unit tests and then provided instructions on how to run the tests via a shell script. However, I'm working with some students that don't have access to computers at home, so I decided to try and use an Jupyter notebook environment (via mybinder.org) to allow them to do the same thing. I've got most of it working already via some ipywidgets and a helper script that runs the unit tests on some arbitrary set of code.
As far as the cells that have been run, the input and output caching system described at https://ipython.readthedocs.io/en/stable/interactive/reference.html#input-caching-system might be useful. (Examples of its use at https://stackoverflow.com/a/27952661/8508004 ). It works in Jupyter notebooks as shown below. (The corresponding notebook can be viewed/accessed here.)
Because #A. Donda raised the issue of markdown in the comments below, I'll add here that nbformat provides related abilities that works with saved notebook files. Reading a saved notebook file with nbformat allowa getting cells and content, no matter if it is code or markdown, and sorting whether the cells are markdown or code, etc.. I have posted a number of examples of using nbformat on the Jupyter Discourse forum that can be seen listed via this search here. It offers more utility than the related json.load(open('test.ipynb','r')) command, highlighted in a comment here to read a notebook file because of the additional notebook context automatically included.
If you want to capture the contents of a specific cell to access it from another one, one workaround seems to bee to write the contents of the first cell to file when executing it and later loading the resulting file (i.e. the text giving the former cell's content) inside the latter cell, where the content of the former cell is required.
Cell's contents can be saved to file (when executing the respective cell) via the command
%%writefile foo.py
which has to be placed at the beginning of a cell. This results in the cell's content (in which the upper command is executed) being saved to the file foo.py and it's just a matter of later reading it in, again.
The output of a cell can be made available more easily:
Just place %%capture output in the first line of a cell. Then, the output of the cell (after execution) is going to be saved as a string to the variable output and can be used like any standard python string-variable.
References:
Programmatically get current Ipython notebook cell output? and
https://nbviewer.jupyter.org/github/ipython/ipython/blob/1.x/examples/notebooks/Cell%20Magics.ipynb
I found a solution to this that can get the output of any cell. It requires running some Javascript.
Here is the code that you can put in a cell and run it an it will generate a Python variable called cell_outputs which will be an array of the cell outputs in the same order as they appear on the page. The output of the cell executing this code, will have an empty string.
%%js
{
let outputs=[...document.querySelectorAll(".cell")].map(
cell=> {
let output=cell.querySelector(".output_text")
if(output) return output.innerText
output=cell.querySelector(".rendered_html")
if(output) return output.innerHTML
return ""
}
)
IPython.notebook.kernel.execute("cell_outputs="+JSON.stringify(outputs))
}
If you need the output of a specific cell, just use its index, eg: cell_outputs[2] in your Python code to access the output of cell #3.
I tested this on my Jupyter notebook 6.0.3 (recent install via the Anaconda community edition) on Google Chrome. The above could should work fine on any modern browser (Chrome, Firefox or Edge).

Writing an IPython notebook extension that can run an analysis on every cell

I am trying to write an IPython notebook extension that will run analysis on the code in each cell and will then output messages via the logger (this could be for example a PEP8 analysis, to take a simple example). The catch is that I want to be able to load the extension once at the top of the notebook and have it apply to all cells.
It is possible to define custom line and cell magics (see here) but one has to put the magic command in each cell whereas I would like it to apply to all code cells.
I also found that it's possible to register a class that pre-filters code input, using ip.prefilter_manager.registre_transformer but that operates on a line by line basis which is not good enough.
Does anyone have any suggestions on how to register a function that can analyze the content of all cells, when they are executed, and add logger messages to the output of that given cell?
I know there is a PEP8 extension for IPython notebook but this requies that the magic %%pep8 command be put inside each cell to be checked, and I want to change this to be more of a global setting in the notebook.

Categories