#!/usr/bin/env python # coding: utf-8 # This [Towards Data Science blog](https://towardsdatascience.com/interactive-controls-for-jupyter-notebooks-f5c94829aee6) has a lot of useful information. Also need to run `jupyter nbextension enable --py widgetsnbextension`. # In[1]: import ipywidgets as widgets from ipywidgets import interact, interact_manual # This is similar to the Groovy scripts of Active Choices parameters. # In[2]: 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`. # In[3]: selected_state = None @interact def show_states(state=states()): global selected_state print(f'You selected {state}') selected_state = state # In[4]: @interact def show_cities(city=cities(state=selected_state)): print(selected_state) print(f'You selected {city}') # Or using the `observe` function. # In[5]: # 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) # 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.