PyNoon Plus Lesson 2 - Tutorial

This tutorial will cover building user interfaces in Jupyter notebooks.

Setup

  1. Make a new notebook for this lesson
  2. What’s the first thing to do? RENAME IT!
  3. Name it pynoon_plus_3.ipynb

UI Widget Basics

To get started, let’s import the panel module that provides widgets for building user interfaces

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

Buttons

Now let’s make a button:

submit_button = pn.widgets.Button(name='Submit')
submit_button
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,
)

Each time you click the button, the current value of the text input is displayed.

Bringing it all together a self-contained form

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

Deploying our app

Preparing the app notebook

import panel as pn

pn.extension()

...interface...

layout.servable()

Converting the app notebook to html

From your original notebook (important to avoid including the !panel convert line in your app notebook):

!panel convert "drive/MyDrive/Colab Notebooks/pynoon_plus_3_app.ipynb" --to pyscript --out app_output

Going further with user interfaces