This page demonstrates how to embed Jupyter components into any web page
using
@datalayer/jupyter-embed.
Simply include the jupyter-embed.lazy.js javascript and add HTML
elements with the appropriate data attributes. The examples below showcase
various embedding options like cell, notebook and output display and can
be executed in live.
Made with ❤️ by Datalayer - Part of the Jupyter UI repository (MIT License).
# Calculate the first 100 Fibonacci numbers
def fibonacci(n):
fib = [0, 1]
for i in range(2, n):
fib.append(fib[i-1] + fib[i-2])
return fib[:n]
print(fibonacci(100))
<div
data-jupyter-embed
data-jupyter-embed="cell"
data-jupyter-height="200px"
data-jupyter-auto-execute="true"
>
<code data-jupyter-source-code>
# Calculate the first 100 Fibonacci numbers
def fibonacci(n):
fib = [0, 1]
for i in range(2, n):
fib.append(fib[i-1] + fib[i-2])
return fib[:n]
print(fibonacci(100))
</code>
</div>
import sys
print(f"Python version: {sys.version}")
# Welcome to Jupyter Embed!
This is a **markdown** cell with:
- Rich formatting
- Lists
- And more!
> 👉 Double-click me to edit.
import numpy as np
import matplotlib.pyplot as plt
# Create data
x = np.linspace(-5, 5, 50)
y = np.linspace(-5, 5, 50)
X, Y = np.meshgrid(x, y)
Z = np.sin(np.sqrt(X**2 + Y**2))
# Create 3D plot
fig = plt.figure(figsize=(8, 6))
ax = fig.add_subplot(111, projection='3d')
surf = ax.plot_surface(X, Y, Z, cmap='viridis', alpha=0.8)
ax.set_xlabel('X axis')
ax.set_ylabel('Y axis')
ax.set_zlabel('Z axis')
ax.set_title('3D Surface: sin(√(x² + y²))')
fig.colorbar(surf)
plt.show()
<div
data-jupyter-embed="output"
data-jupyter-height="200px"
data-jupyter-auto-run="true"
>
<code data-jupyter-source-code>
import numpy as np
import matplotlib.pyplot as plt
# Create data
x = np.linspace(-5, 5, 50)
y = np.linspace(-5, 5, 50)
X, Y = np.meshgrid(x, y)
Z = np.sin(np.sqrt(X**2 + Y**2))
# Create 3D plot
fig = plt.figure(figsize=(8, 6))
ax = fig.add_subplot(111, projection='3d')
surf = ax.plot_surface(X, Y, Z, cmap='viridis', alpha=0.8)
ax.set_xlabel('X axis')
ax.set_ylabel('Y axis')
ax.set_zlabel('Z axis')
ax.set_title('3D Surface: sin(√(x² + y²))')
fig.colorbar(surf)
plt.show()
</code>
</div>
<div
data-jupyter-embed
data-jupyter-embed="terminal"
data-jupyter-height="300px"
data-jupyter-color-mode="dark"
></div>
<div
data-jupyter-embed
data-jupyter-embed="notebook"
data-jupyter-path="test.ipynb"
data-jupyter-height="300px"
data-jupyter-show-toolbar="true"
></div>
<div data-jupyter-embed="viewer" data-jupyter-height="400px">
<script type="application/json">
{
"cells": [
{
"cell_type": "markdown",
"source": ["# My Notebook"]
},
{
"cell_type": "code",
"source": ["print('Hello!')"]
}
],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 4
}
</script>
</div>
<!-- Option 1: Data attributes on the script tag -->
<script
src="jupyter-embed.lazy.js"
data-jupyter-server-url="https://oss.datalayer.run/api/jupyter-server"
data-jupyter-token="60c1661cc408f978c309d04157af55c9588ff9557c9380e4fb50785750703da6"
data-jupyter-auto-start-kernel="true"
></script>
<!-- Option 2: JSON configuration -->
<script type="application/json" data-jupyter-embed-config>
{
"serverUrl": "https://oss.datalayer.run/api/jupyter-server",
"token": "60c1661cc408f978c309d04157af55c9588ff9557c9380e4fb50785750703da6",
"defaultKernel": "python3",
"lazyLoad": true,
"theme": "light"
}
</script>
// Configure the connection
JupyterEmbed.configureJupyterEmbed({
serverUrl: 'https://oss.datalayer.run/api/jupyter-server',
token: '60c1661cc408f978c309d04157af55c9588ff9557c9380e4fb50785750703da6',
defaultKernel: 'python3',
lazyLoad: true
});
// Manually initialize embeds
JupyterEmbed.initJupyterEmbeds();
// Initialize embeds added after page load (e.g., in SPA)
JupyterEmbed.initAddedJupyterEmbeds(container);
// Render a component programmatically
const element = document.getElementById('my-cell');
JupyterEmbed.renderEmbed(element, {
type: 'cell',
source: 'print("Hello!")',
autoExecute: true
});
// Clean up
JupyterEmbed.destroyJupyterEmbeds();