I want to embed the HTML output of Jupyter, in my own web page. The reason for this is primarily, so that I can use Jupyter from my own webapp - and also access my research notebooks from anywhere in the world - via the internet.
A typical use case scenario would be that I click on a button on my page, and an iframe will be inserted in my page; Jupyter will then be launched at the backend (if not already running), and the output of Jupyter will be 'piped' to the iframe - so that I can use Jupyter from within my page.
The naive solution it appeared, was to use <iframe>
, but there were two problems:
- The iframe cross domain policy problem
- Jupyter generated a one time authentication token when first launched
Is there anyway I can overcome these issues, so I can embed the output of Jupyter in my own web page?
you need to check nbconvert - https://github.com/jupyter/nbconvert
there you have 2 options.
here is short code : if you want to show already generated:
from nbconvert.preprocessors import ExecutePreprocessor import nbformat from nbconvert import HTMLExporter from nbconvert.preprocessors.execute import CellExecutionError src_notebook = nbformat.reads(ff.read(), as_version=4) #where ff is file opened with some open("path to notebook file")
html_exporter = HTMLExporter() html_exporter.template_file = 'basic' #basic will skip generating body and html tags.... use "all" to gen all.. (body, resources) = html_exporter.from_notebook_node(src_notebook) print(body) #body have html output
if you want also to run notebook, then :
from nbconvert.preprocessors import ExecutePreprocessor import nbformat from nbconvert import HTMLExporter from nbconvert.preprocessors.execute import CellExecutionError src_notebook = nbformat.reads(ff.read(), as_version=4) #where ff is file opened with some open("path to notebook file")
ep = ExecutePreprocessor(timeout=50, kernel_name='python3') ep.preprocess(src_notebook, {}) html_exporter = HTMLExporter() html_exporter.template_file = 'basic' #basic will skip generating body and html tags.... use "all" to gen all.. (body, resources) = html_exporter.from_notebook_node(src_notebook) print(body) #body have html output