Python Jupyter Notebook: specify cell execution order

I have a Jupyter laptop. In cell 1, I defined a lot of functions that need to be run before other things. Then in the following cells, I begin to cite the result. However, when I convert to HTML, this layout is ugly. Readers must scroll for 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 this order, because I need these functions.

So my question is: is there a way to control the order in which the cells run after I click the Run All button? 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 that the Jupyter is something like "run cell 20".

Just wondering if this is doable. Thank.

+8
source share
4 answers

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

+4
source

Such functionality, as far as I know, is not yet available in Jupyter. However, if you are really worried about having many function definitions at the beginning and want to hide them, you can make the following alternative:

  • Define functions in a Python script.
  • Add script execution to the first encoding cell of your laptop
  • Add the remaining code to consecutive notebook cells
  • Optionally, show its contents at the end of the notebook for easy viewing.
+3
source

Runtools nbextension, .

0

Check the execution of dependency . In this case, you can determine the dependencies of the execution order of your cells.

To use tags in your cells: View - Cell Toolbar - Tags

For instance:

Add tags to your cells

Cell 1 - #HTML, => functions

print(txt)

...

Cell 20 - # Functions

txt = 'functions'

When you start cell 1, it will output “functions” because it will start cell 20 first.

0
source

Source: https://habr.com/ru/post/1661296/


All Articles