Dynamic Widgets

Dynamic widgets

Mercury allows you to update widgets during app running. Here is an example for dynamic widgets. There are two Select widgets. Choices in the second widget depend on selected values in the first one.

Required packages

You will need to install mercury package to run this example.


Let's import required packages:

import mercury as mr

We will define list and dict with small and capital letters:

letters = ["small", "capital"]
example_letters = {
    "small": ["a", "b", "c", "d"],
    "capital": ["A", "B", "C", "D"]

We will choose letter size in the first Select:

size = mr.Select(value="small", choices=letters, label="Small or capital?")

We will choose example letter in the second Select:

example = mr.Select(value=example_letters[size.value][0], 
                    label="Select letter")

The last cell with print selected values:

print(f"Example {size.value} letter is {example.value}")

The screenshot with code in the Jupyter Notebook:

Python notebook with dynamic widgets

Please remember, that widget update doesn't trigger automatic cell re-execution during notebook development in Jupyter Notebook.

Cells are automatically re-executed in Mercury.

Mercury App

Please start Mercury in the same directory as notebook:

mercury run 

The Mercury Site is available at Please open notebook with dynamic widgets, you can play with widgets and see the updated output:

Mercury App with dynamic widgets

The notebook code is available in mercury-examples (opens in a new tab) GitHub repository.