#!/usr/bin/env python # coding: utf-8 # By using Juyper-flex with Voila, you can create dashboards that enable viewers to change underlying parameters and see the results immediately. This is done by adding runtime: a live Jupyter kernel and then adding one or more input controls that dynamically drive the appearance of the components within the dashboard. # # Voila turns a Jupyter Notebook into an interactive document. It's important to note that interactive documents need to be deployed using Voila to be shared broadly, whereas static html documents generated by `nbconvert` that can be attached to emails or served from any standard web server. # # ## Getting Started # # The steps required to make an interactive dashboard can be summarized as: # # 1. Create one Section, level-2 markdown header (`##`) # 2. On this section add some code that shows `ipywidgets` controls and tag the cell with `body` (same as any other content) # 3. Add one or multiple regular Jupyter-flex Sections (`##`) # 4. Create an [Output ipywidget](https://ipywidgets.readthedocs.io/en/latest/examples/Output%20Widget.html) that holds the outputs for the dashboard # 5. Use the `interact()` or `observe()` functions from `ipywidgets` to update Output widget # 6. Tag a code cell that outputs the Output widget with `body` # 7. Run the notebook using `voila` # # ## Simple example # # Let's do a simple example using `ipywidgets` to generate a random distribution and plot a histogram using `matplotlib`. The dashboard will contain 3 widgets to control the mean and standard deviation of the data and number of bins of the histogram. # In[ ]: import numpy as np import ipywidgets as widgets from IPython.display import clear_output, display import matplotlib.pyplot as plt from IPython.display import set_matplotlib_formats get_ipython().run_line_magic('matplotlib', 'inline') set_matplotlib_formats('svg') np.random.seed(42) #
Matplotlib output format
#Since we are gonna use matplotlib to plot the histogram we set the format to be SVG, this looks better on the final dashboard because the plot is more responsive.
#For more information on the different plotting library options see Plotting.
## Note that widgets shown here as just displays, not connected to a kernel. #
#Terminal
# ```bash # $ voila --template=flex mpl-histogram.ipynb # ``` # # This will open a browser window and the result show look like this, click on the image below to open this example on [binder.org](https://mybinder.org/): # # [![Jupyter-flex voila widgets](/assets/img/screenshots/widgets/mpl-histogram.png)](https://mybinder.org/v2/gh/danielfrg/jupyter-flex/0.6.3?urlpath=%2Fvoila%2Frender%2Fexamples%2Fwidgets%2Fmpl-histogram.ipynb) # ## Examples # # More examples powered by Voila and that use different ipywidgets like [bqplot](https://github.com/bloomberg/bqplot), [ipyleaflet](https://github.com/jupyter-widgets/ipyleaflet) and [qgrid](https://github.com/quantopian/qgrid). # #