Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically generate markdown output in Jupyter notebooks?

People also ask

How do I render markdown in Jupyter notebook?

You can change the cell type of any cell in Jupyter Notebook using the Toolbar. The default cell type is Code. To use the Keyboard Shortcuts, hit the esc key. After that, you can change a cell to Markdown by hitting the m key, or you can change a cell to Code by hitting the y key.

How do I export output from Jupyter?

To export the JNaaP with code present, click File, Download as, HTML (. html). The top line (%%capture) ensures the output of the nbconvert HTML export line will not display. The bottom line exports the notebook to HTML.

Is markdown allowed in Jupyter notebook?

Text can be added to Jupyter Notebooks using Markdown cells. You can change the cell type to Markdown by using the Cell menu, the toolbar, or the key shortcut m. Markdown is a popular markup language that is a superset of HTML.

How do I run a Jupyter notebook script automatically?

Open a Jupyter notebook from the left sidebar. Click on the Scheduler icon either from the left sidebar tab or from the top toolbar of the Jupyter notebook. The left sidebar displays the Schedule(s) and Run History tabs as shown below. To view the active schedules, click Schedule(s) tab.


The functions you want are in the IPython.display module.

from IPython.display import display, Markdown, Latex
display(Markdown('*some markdown* $\phi$'))
# If you particularly want to display maths, this is more direct:
display(Latex('\phi'))

You are basically asking for two different things:

  1. Markdown cells outputting code results.

    I'd like to count some stuff, generate some results and include them in markdown. [...] I'd like to have a template in markdown and insert values generated by the program in the notebook

  2. Code cells outputting markdown

    I'd like such command: print '$\phi$' to generate phi symbol, just like in markdown.

Since 2. is already covered by another answer (basically: use Latex() or Markdown() imported from IPython.display), I will focus on the first one:


1. Markdown Template with inserted variables

With the Jupyter extension Python Markdown it actually is possible to do exactly what you describe.

Installation instructions can be found on the github page of nbextensions. Make sure you'll enable the python markdown extension using a jupyter command or the extension configurator.

With the extension, variables are accessed via {{var-name}}. An example for such a markdown template could look like this:

Python Code in Markdown Cells

The variable a is {{a}}

You can also embed LateX: {{b}} in here!

Even images can be embedded: {{i}}

Naturally all variables or images a, b, i should be set in previous code. And of course you may also make use of Markdown-Latex-style expressions (like $\phi$) without the print command. This image is from the wiki of the extension, demonstrating the capability.

example from wiki


Further info on this functionality being integrated into ipython/jupyter is discussed in the issue trackers for ipython and jupyter.