This Towards Data Science blog has a lot of useful information. Also need to run jupyter nbextension enable --py widgetsnbextension
.
import ipywidgets as widgets
from ipywidgets import interact, interact_manual
This is similar to the Groovy scripts of Active Choices parameters.
def states():
return ['Sao Paulo', 'Minas Gerais', 'Acre']
def cities(state):
if state == 'Sao Paulo':
return ['Sao Paulo', 'Campinas', 'Barretos']
elif state == 'Minas Gerais':
return ['Betim', 'Belo Horizonte']
elif state == 'Acre':
return ['Acrelandia', 'Rio Branco']
else:
return ['UNKNOWN']
The states
can be turned into a widget with interact
.
selected_state = None
@interact
def show_states(state=states()):
global selected_state
print(f'You selected {state}')
selected_state = state
interactive(children=(Dropdown(description='state', options=('Sao Paulo', 'Minas Gerais', 'Acre'), value='Sao …
@interact
def show_cities(city=cities(state=selected_state)):
print(selected_state)
print(f'You selected {city}')
interactive(children=(Dropdown(description='city', options=('Sao Paulo', 'Campinas', 'Barretos'), value='Sao P…
Or using the observe
function.
# initial value
states_dropdown = widgets.Dropdown(options=states())
cities_dropdown = widgets.Dropdown(options=cities(state=states_dropdown.value))
# react to an event
def update_cities(state):
if hasattr(state, 'new'):
state = state.new
cities_dropdown.options = cities(state=state)
# create reactivity (same as reference parameter = cities in Jenkins/Active Choices)
states_dropdown.observe(update_cities, 'value')
# add a button (to call Jenkins?)
def submit_jenkins_job(event):
selected_state = states_dropdown.value
selected_city = cities_dropdown.value
print(f'Submitting a job with parameters state={selected_state}, city={selected_city}')
button = widgets.Button(
description='Submit Job',
disabled=False,
tooltip='This will use Jenkins API with the auth settings...'
)
button.on_click(submit_jenkins_job)
# render
display(states_dropdown, cities_dropdown, button)
Dropdown(options=('Sao Paulo', 'Minas Gerais', 'Acre'), value='Sao Paulo')
Dropdown(options=('Sao Paulo', 'Campinas', 'Barretos'), value='Sao Paulo')
Button(description='Submit Job', style=ButtonStyle(), tooltip='This will use Jenkins API with the auth setting…
Submitting a job with parameters state=Sao Paulo, city=Sao Paulo Submitting a job with parameters state=Minas Gerais, city=Belo Horizonte
The function can use pandas, matplotlib, notebook widgets for images, etc. Pretty much anything available for Notebooks, or custom code/widgets too. More options and more extensible than with Jenkins.