I am trying to export a Jupyter notebook as a static website.
So far I had been using jupyter nbconvert --execute --to html
without any problem.
However, I would like to include ipywidgets
. The widget in question, ipywidgets.AppLayout
, contains an IPython.core.display.HTML
which is causing problems.
Here is the relevant part:
from ipywidgets import AppLayout
from ipywidgets import HTML, Layout, Dropdown, Output, Label, Button
from IPython.display import display, clear_output
widget_1 = Dropdown(
options=[1,2,3,4],
description='1:'
)
widget_2 = Dropdown(
options=["A","B","C","D"],
description='2:'
)
html_out = Output()
with html_out:
display(df.loc[widget_1.value, widget_2.value].HTML())
# Notice that it's an HTML object:
# type(df.loc[widget_1.value, widget_2.value].HTML())
# IPython.core.display.HTML
#
# And that it was already in memory/isn't generated at that moment:
# ...
# def HTML(self):
# return self.__HTML
button = Button(description='Display')
def on_button_clicked(b):
with html_out:
clear_output(True)
display(df.loc[widget_1.value, widget_2.value].HTML())
button.on_click(on_button_clicked)
app=AppLayout(
left_sidebar=VBox([
widget_1,
widget_2,
button
]),
center=html_out
)
app
I have a pandas dataframe with objects of the same class in each cell.
The objects have a method, HTML
, that returns a IPython.core.display.HTML
that had already been generated and is in memory. Here is the code to the method:
def HTML(self):
return self.__HTML
I want to display that depending on the selected values from widget_1
and widget_2
.
When running the notebook in JupyterLab, everything works well, but by exporting to HTML using the previous method the widgets don't render. I know this is expected behavior, but I wonder how I can obtain a working widget.
I tried using embed_minimal_html
and it does generated most of the widget, including the default state of html_out
but it can't be updated using the widgets.
from ipywidgets.embed import embed_minimal_html
embed_minimal_html('export.html', views=[app], title='Widgets export')
I then tried voila
which renders the whole notebook (which is even better) but, again, the html_out
is not updated with the widgets.
Finally, I've seen that JupyterLite
can be used to make notebooks run on the web leveraging Pyodide
, but in that case the whole notebook is interactive and I want a static notebook with interactive widgets, or no notebook at all, just the widgets.
Any idea how I can include those objects, that are already in memory, in order to have a working widget in the HTML?
EDIT1: Added mention to Voila