Jupyter widgets vs JupyterLab Extension? - python

I know that JupyterLab is the next-generation web-based UI for Project Jupyter and it's based on the Jupyter Notebook and Architecture, and it will eventually replace the classic Jupyter Notebook.
I am a bit confused about the differences and relationships between Jupyter widgets and JupyterLab Extensions:
Will JupyterLab Extensions eventually replace those Jupyter widgets for using interactive Notebook?
Is Jupyter Widgets only for classic Jupyter Notebook? Or is Widgets just one type of the Extensions for JupyterLab?

I'm not an expert on Jupyter extensions, but I think the following is accurate (please correct me if someone knows otherwise).
Jupyter widgets are actually implemented using an extension.
The main difference between widgets and other extensions is that
the Python kernel in a notebook can easily interact with a widget
-- the kernel can "call in" to the widget and the widget can "call back" to the kernel. For example the kernel can change the text label for a button in the widget and a button press on the widget can cause
Python code to execute in the kernel.
For example the following notebook creates a widget with a click callback to python which calls back to Javascript to change the appearance of the widget:
https://github.com/AaronWatters/jp_proxy_widget/blob/master/notebooks/hello%20proxy%20world.ipynb
Extensions do not automatically build in ways for a Python kernel process in a notebook to interact with the extension instance, although it is possible to implement them.

Related

Upon restarting a Jupyter notebook, automatically run 1st/nth cell only (to load modules and other variables), ideally using a keyboard shortcut

In addition to loading required modules, in the first cell of the jupyter notebook, I usually load variables (e.g. metadata, tables etc.). So every time when I restart a jupyter notebook, I need to run the 1st cell of the notebook. I know this is not a big issue, but sometimes can be an annoyance. For example, if I would be working at the end of the notebook and need to restart it, I would have to scroll all the way up, run the first cell, and then scroll all the way back to resume the work.
I hope there is a way to automatically run 1st (or any n'th) cell of a jupyter notebook when it is restarted. I wonder if anybody knows if such a functionality exists.
Install jupyter notebook extensions (see here how it is done: https://jupyter-contrib-nbextensions.readthedocs.io/en/latest/install.html ).
Use the Initialization cells extension (https://jupyter-contrib-nbextensions.readthedocs.io/en/latest/nbextensions/init_cell/README.html ).
It allows you to mark one or more cells as 'Initialisation cells' that will run when the notebook is opened or on clicking the (then added) 'initialization button' in the main toolbar.
The cell toolbar selector can be found under View > Cell Toolbar > Initialization Cell.
Not 100% what you are looking for as you need to click an extra button after restarting the notebook. On the other hand, scrolling up and down is avoided and it is an easy fix.

Trace Statement Execution in Jupyter Notebooks?

I am currently writing some Python code in a Jupyter notebook and I wonder if there is any possibility to track the execution of statements directly in Jupyter? I know there is this module called "trace": https://docs.python.org/3/library/trace.html#trace.Trace.results But it seems to me that with "trace" it is only possible to show the flow of execution in the console or to export it to a new file. However, I would like to display it directly in Jupyter Notebooks. Is this possible - with or without the mentioned module - and if yes how?
Thanks for your help.
For something along the lines of debugging code, you may be interested in the cell magic %%debug, discussed here to open a debugger in the notebook and use pdb commands. Or PixieDebugger if you need something fancier.
You may also be interested in the variable inspector extension for JupyterLab. There is (old) an animated example of using it here. You can easily try it out by clicking on launch binder badge there and starting a notebook. Then right-click in the open notebook and select Open Variable Inspector from the list. The animation will show you how to drag the tabs to arrange them side by side on your screen. Now as you run code, you'll see the variables updated.

How to switch environments for open jupyter notebook

If open a python jyputer notebook in vscode, then try to execute the notebook (so a kernel starts) and realize I need to activate a different environment, there doesn't seem to be a way to switch environments without completely quitting VSCode and coming back in.
If can switch environments in the terminal window or using the environment picker in the lower left, but it there doesn't seem to be a way to quit the current kernel and have it re-launch under the new environment. I've tried using the "Restart Kernel", closing the notebook and re-opening.
Is there a way to do this?
I'm a developer on this extension. The intention was for this feature to function as the Python Interactive Window did, where you could use the VSCode Interpreter picker to change out an open notebook to a new kernel. However it appears that we have a hitch that we shipped with this functionality. There is already an issue open in our github here which I believe that you filed (just going on user name match :)):
https://github.com/microsoft/vscode-python/issues/7862
That will be the right place to track our progress on fixing this, and it's on our immediate backlog.

JupyterLab: Run all cells below

In the Jupyter Notebook I could use the following command to automatically execute all cells below the current cell.
from IPython.display import Javascript
display(Javascript('IPython.notebook.execute_cells_below()'))
However, this doesn't seem to work with JupyterLab. How can I make this work for JupyterLab?
(I want to automatically execute all cells below after a button click occured. The original code for this was posted here: IPython - Run all cells below from a widget)
It is built-in. Click the run menu at the top-left and select "Run Selected Cell and All Below".
Installing this Jupyter extension in VS-Code, both the commando "Notebook: Execute Cell and Below" and the button in the top-left of each cell are built-in.
The commands can be opened with "CTRL + SHIFT + P".
Commando "Notebook: Execute Cell and Below"
Button in the top-left of each cell:
This might be possible with ipylab. However, in JupyterLab 3.2.3 ipylab does not seem to work.
Also see
How to cause Jupyter Lab to save notebook (programmatically)
with ipylab you can run (some) JupyterLab backend commands in frontend, so installing ipylab in jupyter:
import ipywidgets as widgets
app = JupyterFrontEnd()
def run_all(ev):
app.commands.execute('notebook:run-all-below')
button = widgets.Button(description="Run all below")
button.on_click(run_all)
display(button)
And more, you can create your own Jupyterlab Scripts (Like a matplotlib instruction)

Open jupyter in firefox window with no decorations

I am using Jupyter in Firefox on OSX. Is there a way I can ensure the notebook opens in a window without any of the toolbars etc of firefox?
I currently use it for julia by running
using IJulia
notebook()
but I guess the kernel should not matter.
Thanks.
Perhaps the following will be useful (untested):
https://support.mozilla.org/en-US/questions/1029353
By the way, you can launch the notebook directly from the terminal using
ipython notebook # IPython version < 4
or
jupyter notebook # Jupyter version >= 4
[
or
ipython notebook --profile julia
if you are running IPython < 3, in which case I recommend upgrading
]
Also note that in Chrome there is a nice mode with the toolbars hidden via
View -> Enter Presentation Mode
(I do not see anything like this in Firefox.)
You can then manually hide the IPython/Jupyter headers with View->Hide toolbar and View->Hide header from within the IPython/Jupyter menu.
In this way you can get the whole screen dedicated to your notebook with (almost) no visual clutter.

Categories