Taipy GUI in Jupyter Notebooks

notebook before

Taipy out-of-the-box support of Jupyter Notebooks provides us with:

  1. Taipy  full suite of data viz and interactivity tools in our notebooks and
  2. A sandbox for experimenting with Taipy’s many features.

Introduction

When working with Taipy in Python scripts (.py), we would usually rerun the script whenever we make modifications to our code. The Jupyter Notebook (.ipynb) equivalent would be to restart the kernel and rerun all cells, which would not make for a pleasant coding experience!

To remedy this and to effectively use Taipy in Jupyter Notebook, we should make use of two simple functions for updating the front-end after we make changes to our code:

  1. Page.set_content – After updating the contents of a page; and
  2. Gui.reload – After modifying a variable used in a page.

Modifying Page Content

The following code demonstrates creating a simple Taipy web application in Jupyter Notebook:

from taipy.gui import Gui, Markdown
page_md = Markdown("Page content")
gui = Gui(page=page_md)
gui.run()
Now, when we want to change the contents of page_md, we may be tempted to (incorrectly) modify and rerun our existing Page definition cell like so:
# do NOT rerun this cell
page_md = Markdown("Page content (updated)")

Doing so will NOT have the intended effect on our web page. Instead, what we should do is use the Page.set_content method (e.g. page_md.set_content(”New content”)), like so:

from taipy.gui import Gui, Markdown
page_md = Markdown("Page content")
gui = Gui(page=page_md)
gui.run()
# this new cell is the only cell that needs to be run!
page_md.set_content("Page content (updated)")
Just running the new cell (and a browser refresh) will **properly update our web page**.
notebook set content

Modifying Variable

Another common thing we’d want to do is to modify the value of a variable that we’re using in our Page definition. Take the following Jupyter Notebook as an example:

from taipy.gui import Gui, Markdown
title = "GUI in Notebooks"
page_md = Markdown("<|{title}|>")
gui = Gui(page=page_md)
gui.run()

# this new cell is the only cell that needs to be run!
title = "Taipy GUI in Jupyter Notebook"
gui.reload()
In this example, we want to change the value of the titletitle = “Taipy GUI in Jupyter Notebook”).
To inform Taipy of the changes, we must call Gui.reload (e.g. gui.reload()) so that the latest values of the variables are loaded from the Jupyter session namespace. After running the cell, refresh your browser to see the updated changes.
notebook gui reload

This applies not just to variables in pages, but also to callback functions (e.g. on_change or user-defined callbacks).

Conclusion

Taipy is committed to providing full-fledged web GUIs from both Python scripts and notebooks. As a recap, to efficiently use Taipy GUI within notebooks, be sure to use:

  1. Page.set_content – After updating the contents of a page; and
  2. Gui.reload – After modifying a variable used in a page.

 

Zaccheus Sia
Zaccheus Sia

Data Scientist at Knowledge Touch Pte Ltd