Package: observable-jupyter-widget
observable-jupyter-widgetRun Observable notebooks in Jupyter, sending values between Python and JavaScriptObservableis pretty great. But sometimes you need Python! Or, more often, what you already have is Jupyter. What if you could use your (or someone else's) Observable notebooks in Jupyter, passing values back and forth?Allow viewers of a Jupyter notebook use powerful Observable inputs like theFIPS county code brushto specify Python values interactively (seeexample Colab notebook)Display data calculated in Jupyter on interactive D3 plots (see gallery)Quickly iterate on data visualization on observablehq.com: publish an update to an Observable notebook, wait a few seconds, and refresh the Jupyter web page. That's right, no kernel restarts!Or create powerful interactive widgets that request additional data from Python without building a webapp. Display a map that limits client-side data by requesting more when the user pans the map from a server-side Jupyter kernel with plenty of RAM.This library is similar toobservable-jupyter, which allows feeding Python values into an Observable notebook once per embed. Unlike that library, this widget version allows new inputs to be sent in and brings Observable cell outputs back to Python. It also integrates with the Jupyter Widget ecosystem, so e.g. callbacks can run every time new values are produced in the embed.UsageInstall the package and import the module.!pipinstallobservable_jupyter_widgetimportobservable_jupyter_widgetInstantiate a widget object and display it by writing the variable name on the last line of a cell without a semicolon.Pass in the Observable notebook you want to render and optionally include which cells to display, input Python values to substitute into the Observable notebook, and which Observable cells to report the output values of.w=observable_jupyter_widget.ObservableWidget('@ballingt/embedding-example',cells=['vegaPetalsWidget','viewof minSepalLength','viewof minSepalWidth','extraCell'],# optionalinputs={'extraCell':123},# optionaloutputs=['minSepalLength','extraCell']# optional)wWidgets have a.valueattribute which is a dictionary of values from Observable cells.print(w.value)Using theredefinemethod you can redefine Observable inputs to new values:w.redefine(extraCell=10000)See exampleColab notebookLimitationsObservableWidgets only run when onscreen#2For security (to prevent embedded notebooks from running untrusted Python code) an embedded Observable notebook runs in an iframe.
The observable runtime is runs onAnimationFrame, an event that never happens if the iframe is offscreen in most browsers.Embed output may not be ready when the next Jupyter cell runs#1Observable notebooks take time to run and resolve their.valuevalue (any amount of time, depending on the notebook) but the Jupyter kernel keeps right on chugging.
When using "Restart and Run All" menu item in Jupyter, or even when quickly executing consecutive cells manually with option-enter, the.valueattribute may still be None (the initial value) instead of a dictionary mapping cell names to output values.To get around this, use ipython_blocking cell magic %block along with a function that evalutes to True once the value is ready, or just don't run all cells at once!importipython_blockingw=ObservableWidget(...)observable_output_ready=lambda:w.value!=None---%blockobservable_output_ready---print(w.value)Embeds do not execute in non-interactive notebook execution environments like PapermillObservableWidget works great for interactive experiences embedded in a Jupyter notebook. Although results of JavaScript interactions are exposed by the.valueattribute, it needs to be viewed by a user to run. If you're using a Jupyter notebook to run scheduled tasks like ETL, try aJuypyter kernel that uses nodeto run JavaScript.InstallationYou can install usingpip:pipinstallobservable_jupyter_widgetIf you are using Jupyter Notebook 5.2 or earlier, you may also need to enable
the nbextension:jupyternbextensionenable--py[--sys-prefix|--user|--system]observable_jupyter_widgetDevelopment InstallationTODO this is fromthe cookiecutter template. It's not wrong, butit's not what I use.Create a dev environment:condacreate-nobservable_jupyter_widget-dev-cconda-forgenodejsyarnpythonjupyterlab
condaactivateobservable_jupyter_widget-devInstall the python code. This will also build the TS package.pipinstall-e".[test, examples]"When developing your extensions, you need to manually enable your extensions with the
notebook / lab frontend. For lab, this is done by the command:jupyter labextension develop --overwrite .
yarn run buildFor classic notebook, you need to run:jupyter nbextension install --sys-prefix --symlink --overwrite --py observable_jupyter_widget
jupyter nbextension enable --sys-prefix --py observable_jupyter_widgetNote that the--symlinkflag doesn't work on Windows, so you will here have to run
theinstallcommand every time that you rebuild your extension. For certain installations
you might also need another flag instead of--sys-prefix, but we won't cover the meaning
of those flags here.How to see your changesTypescript:If you use JupyterLab to develop then you can watch the source directory and run JupyterLab at the same time in different
terminals to watch for changes in the extension's source and automatically rebuild the widget.# Watch the source directory in one terminal, automatically rebuilding when neededyarnrunwatch# Run JupyterLab in another terminaljupyterlabAfter a change wait for the build to finish and then refresh your browser and the changes should take effect.Python:If you make a change to the python code then you will need to restart the notebook kernel to have it take effect.