# Embedding Jupyter Widgets in Other Contexts than the Notebook Jupyter interactive widgets can be serialized and embedded into - static web pages - sphinx documentation - html-converted notebooks on nbviewer Here, we discuss embedding widgets using the custom widget manager in the `@jupyter-widgets/html-manager` npm package. Two embedders are provided: 1. A basic embedder that only embeds standard controls, but can be used on any web page 2. An embedder that uses RequireJS, and can embed standard and custom widgets ## Embedding Widgets in HTML Web Pages The classic notebook interface provides a `Widgets` menu for generating an HTML snippet that can be embedded into any static web page: The menu provides three sets of actions - Save Notebook Widget State and Clear Notebook Widget State - Download Widget State - Embed Widgets ### Save Notebook Widget State A notebook file may be saved with the current widget state as metadata. This allows the notebook file to be rendered with rendered widgets (see the section about Sphinx below, for example). To save a notebook with the current widget state, use the `Save Notebook Widget State` menu item. In order to delete old saved state and save new state to the notebook, do the following in order: 1. Use the `Clear Notebook Widget State` menu and save the notebook. This clears the metadata from the notebook file. 2. Restart the kernel and refresh the page. This clears the old widget state from the widget manager on the page. 3. Create whatever widgets you'd like, and use `Save Notebook Widget State` and save the notebook. This saves the new widget state to the notebook file. ### Embeddable HTML Snippet The `Embed widgets` menu item provides a dialog containing an HTML page which embeds the current widgets. In order to support custom widgets, it uses the RequireJS embedder. This HTML snippet is composed of multiple `

Widget export

""" manager_state = json.dumps(data['manager_state']) widget_views = [json.dumps(view) for view in data['view_specs']] rendered_template = html_template.format(manager_state=manager_state, widget_views=widget_views) with open('export.html', 'w') as fp: fp.write(rendered_template) ``` The web page needs to load RequireJS and the Jupyter widgets HTML manager. You then need to include the manager state in a `