Ipynb's were made for the web

Did you know Python notebooks (.ipynb)'s are just JSON objects that get shown from a web browser? Cells and their outputs are all interactive HTML elements rendered from the JSON. When you execute python code in a python cell, it gets sent to the Jupyter backend service which runs the python code, updates the JSON, and returns the results back to the webpage. This is true for all notebook environments, even in VS Code.
Go Figure!
Subsequently, I built Ipynb2Web and this demo page.
Click a shape!
The converter starts by reading in YAML FrontMatter and then processesing each markdown and code cell. Code cells input and outputs may get conditional treatment using flags.
Click a shape!
Inspired by nbdev and nbConvert.
Markdown Features
Spanning A Single Line
[This text will be highlighted]{.className.highlight #id}
This text will be highlighted
Footnotes
Here is an inline note Inlines notes help keep your thoughts organized where CSS can define how they behave.
Here is an inline note.^[Inlines notes.]{.dootdootdoo}
Here is another inline note. Inlines notes.
and another inline note. Inlines notes.
Wrap Multiple Lines
Wrap content with three colons ::: which convert to spans with {.specific #attributes}
::: {.tip} You say whatever you need to say between the colons :::::: {.info} is an info boxNest Multiple Divs
:::::: {.border} Create a div using three colons and nest them by adding 3 colons outwards. ::: { #idName .className } Your interior div here. ::: ::::::
Code Cells Features
For example, click this blue text. I put javascript right in the middle of this sentence within the .ipynb doc.
System outputs however, do not get rendered (error, install & import messages, etc..)
Running %%Javascript
When you use %%javascript to run JavaScript code in a Jupyter notebook, the code gets executed within a script tag. This means that variables or functions you define will be added to the global scope and are thus accessible to all the other cells (remember that the notebook is actually a webpage).
%%javascript console.log('hello world')
Running the above code in an .ipynb then check your dev tool logs.
In VSCode this can be found by navigating to help>toggle dev tools
Cell flags
Flags have to be on the first row of python cells with spaces between.
- "#collapse_input_open",
- "#collapse_input",
- "#collapse_output_open",
- "#collapse_output",
- "#hide_input",
- "#hide_output",
- "#hide",
- "#export"
Ipynb Magic
Magic does not play nicely with cell flags but is supported
- "%%capture",
- "%%javascript",
- "%%html",
Here's Some Python Output
Missing: pygal, bokeh, holoviews, plotly, prophet, tensorflow, web components.
Ipython
Display Javascript
Using this method Javascript is persisted in the outputs of the cell and will be executed when the document is reloaded.
Information obtained from: Advanced Outputs
eval_js differs from display.Javascript because eval_js expressions are not persisted in the notebook document and will not be executed when the document is reloaded.Integrations
Folium / Leaflet
Folium JavascriptLink
Folium Adding Javascript
Folium How to Use Custom Javascript
Folium Custom HTML JS
Folium Click Event
MPLD3
The mpld3 project brings together Matplotlib, the popular Python-based graphing library, and D3js, the popular JavaScript library for creating interactive data visualizations for the web. The result is a simple API for exporting your matplotlib graphics to HTML code which can be used within the browser, within standard web pages, blogs, or tools such as the IPython notebook.
NVD3 python-nvd3
NVD3 is an attempt to build re-usable charts and chart components for d3.js without taking away the power that d3.js offers you.
This will output the following HTML to render a live chart. The HTML could be stored into a HTML file, used in a Web application
Google Colabs
The output of each cell (while in colabs) is hosted in a separate sandboxed iframe without direct access to the contents of other cells, but they can create BroadcastChannels to communicate between cells on the same page.
This will execute and trigger in realtime in the converted HTML doc:
This will not run on start
This will execute on start (converted HTML doc) and any time you run the cell (in Colabs):
