Jupyter Notebook
To embed a Jupyter Notebook to a page, you can do so with Custom HTML, or using GitHub Code if the notebook is hosted on GitHub.
How to Embed a Jupyter Notebook using Custom HTML?
To embed a Jupyter Notebook:
- Export it to HTML. To export it, after you run your notebook, run the following command in your workspace:
import os
os.system('jupyter nbconvert --to html yourNotebook.ipynb')
- An HTML version of your Jupyter Notebook would be generated in the workspace. Download the HTML file locally.
- Open the HTML file and copy its contents.
- Create a new Custom HTML block in the page where you want to embed a Jupyter Notebook.
- Paste the HTML into it.
Example
The Lorenz Differential Equations¶
Before we start, we import some preliminary libraries. We will also import (below) the accompanying lorenz.py
file, which contains the actual solver and plotting routine.
%matplotlib inline
from ipywidgets import interactive, fixed
We explore the Lorenz system of differential equations:
$$ \begin{aligned} \dot{x} & = \sigma(y-x) \\ \dot{y} & = \rho x - y - xz \\ \dot{z} & = -\beta z + xy \end{aligned} $$Let's change ($\sigma$, $\beta$, $\rho$) with ipywidgets and examine the trajectories.
from lorenz import solve_lorenz
w=interactive(solve_lorenz,sigma=(0.0,50.0),rho=(0.0,50.0))
w
For the default set of parameters, we see the trajectories swirling around two points, called attractors.
The object returned by interactive
is a Widget
object and it has attributes that contain the current result and arguments:
t, x_t = w.result
w.kwargs
After interacting with the system, we can take the result and perform further computations. In this case, we compute the average positions in $x$, $y$ and $z$.
xyz_avg = x_t.mean(axis=1)
xyz_avg.shape
Creating histograms of the average positions (across different trajectories) show that, on average, the trajectories swirl about the attractors.
from matplotlib import pyplot as plt
plt.hist(xyz_avg[:,0])
plt.title('Average $x(t)$');
plt.hist(xyz_avg[:,1])
plt.title('Average $y(t)$');