This tutorial will cover building user interfaces in Jupyter notebooks.
pynoon_plus_3.ipynb
To get started, let’s import the panel
module that
provides widgets for building user interfaces
%pip install panel
, but it’s
pre-installed in Colab.pn.extension()
to finish loading
panel.import panel as pn
pn.extension()
Now let’s make a text field:
text_field = pn.widgets.TextInput()
We can display it:
text_field
And we can get the current contents of the field:
text_field.value
text_field.value
.Now let’s make a button:
submit_button = pn.widgets.Button(name='Submit')
submit_button
pn.bind()
to assign a “callback” function
that should be called whenever the button is clicked.submit_button = pn.widgets.Button(name='Submit')
def on_submit(event):
if not event:
return
return text_field.value
submit_output = pn.bind(on_submit, submit_button)
pn.Column(
submit_button,
submit_output,
)
pn.bind()
will display outputs
returned by the callback.
pn.Column
.pn.bind()
can also bind functions to any widget
updates, like text box changes.Each time you click the button, the current value of the text input is displayed.
text_field = pn.widgets.TextInput()
submit_button = pn.widgets.Button(name='Submit')
def on_submit(event):
if not event:
return
return text_field.value
submit_output = pn.bind(on_submit, submit_button)
layout = pn.Column(
text_field,
submit_button,
submit_output,
)
layout
panel
application with users is
to convert it to a shareable .html
file.html
is the language web pages are written in, so any
user will be able to open the file in their web browser.html
files that can run Python code.pynoon_plus_3_app.ipynb
import panel as pn
pn.extension()
...interface...
layout.servable()
layout
ends with
.servable()
From your original notebook (important to avoid including the
!panel convert
line in your app notebook):
Mount Drive
!panel convert "drive/MyDrive/Colab Notebooks/pynoon_plus_3_app.ipynb" --to pyscript --out app_output
app_output/pynoon_plus_3_app.html
pynoon_plus_3_app.html
in your web browser to use
your app!panel
documentation
Component Gallery
-> Widgets
in the
documentation sidebar)Component Gallery
-> Panes
in the
documentation sidebar)