Hi,
I麓m trying to execute specific cells through a widget and validating some conditions
I only found one example with this method: IPython.notebook.execute_cells_below().
import ipywidgets as w
from IPython.display import Javascript, display
def run_all(ev):
display(Javascript('IPython.notebook.execute_cells_below()'))
button = w.Button(button_style='info',description="Execute `Report")
button.on_click(run_all)
display(button)
credits https://stackoverflow.com/a/32769976
But, what I'm trying to do is something like this
import ipywidgets as w
from IPython.display import Javascript, display
def run_all(ev):
if validation == 1: #validation is a var with a previous assignment.
display(Javascript('IPython.notebook.execute_cell(3)')) # 3 could be the cell Id that i want to execute
else:
display(Javascript('IPython.notebook.execute_cell(4)')) # 4 could be the cell Id that i want to execute
button = w.Button(button_style='info',description="Execute Report")
button.on_click(run_all)
display(button)
Is that possible?, please help me
@rodolfo-flores-tierconnect-com did you find any potential solution for this ?
Was looking for the same thing and your post helped:
grep -rn excecute_cells_below python_folder/site-packages and the list of supported function can be found on .../site-packages/notebook/static/notbook/js/action.js
Hi,
Sorry for the late response.
The file .../site-packages/notebook/static/notebook/js/actions.js has the possible actions that we can do over the cells, but all executing actions are based on the "selected_cell" "Run above", "Run Below", etc. We don't have the Ids of of cell in order to handle them programatically.
Unfortunately I couln't find any solution to solve this.
As solution, I put all my code inside the button action function and my charts and results are being displayed in one cell and one result under the other, it was not what I wanted, but It was my only option for my purpose.
I'm trying Zeppelin as well, the great feature is that it has their API requests at cell level, It could be awesome if we have this feature in Jupyter in the future.
There are some APIs which can run cells identified by numbers, but unfortunately the numbers change if you insert or delete a cell somewhere above.
Oh, good to know that 馃憤
Do you have documentation of those APIs? I would like to explore and try them.
Unfortunately I don't think there is any documentation. We held out on it for far too long on the grounds that it might change, and now the plan is to replace it entirely with Jupyterlab anyway.
But here's a couple of functions which you could use:
@takluyver how can I implement this to be automated ? for example can I make this dependent on Markdown Headings to run or not run the cells under it ?
Lets say I have a notebook with 3 main sections and I want to run only section 1 and 3 and skip all the cells in section 2.
Iterate through cells from Jupyter.notebook.get_cells(). For each cell, check its type using c.cell_type. For Markdown cells, inspect c.get_text() to check for headings. Build a list of code cell indices in each section (or find the start and end indices of the section, if you prefer).
Doing the following helps me to execute the cell right below the code cell. You can also change the values to get cells in other parts of the notebook.
display(Javascript('IPython.notebook.execute_cell_range(IPython.notebook.get_selected_index()+1, IPython.notebook.get_selected_index()+2)'))
from ltp111, excecute the cell in place is display(Javascript('IPython.notebook.execute_cell_range(IPython.notebook.get_selected_index()-1, IPython.notebook.get_selected_index())'))
Most helpful comment
Doing the following helps me to execute the cell right below the code cell. You can also change the values to get cells in other parts of the notebook.
display(Javascript('IPython.notebook.execute_cell_range(IPython.notebook.get_selected_index()+1, IPython.notebook.get_selected_index()+2)'))