Jupyter Notebook Variable In Markdown

Posted on  by admin
  • Jupyter Tutorial

I wrote all the text, symbols (even the arrows!) and equations in the image above on Jupyter notebook’s markdown! Without a doubt, documentation is an essential part of working in Data Science. Example Data Analysis in a Jupyter Notebook. First, we will walk through setup and a sample analysis to answer a real-life question. This will demonstrate how the flow of a notebook makes data science tasks more intuitive for us as we work, and for others once it’s time to share our work.

Jupyter

Jupyter Notebook Print Variable In Markdown

  • Surprisingly, Jupyter Notebooks do not support the inclusion of variables in Markdown Cells out of the box. If you still use Jupyter Notebooks there is a readily solution: the Python Markdown extension. It is part of the nbextensions package which is easy to install and configure.
  • As a data professional, you may want to discuss results in a Jupyter Notebook markdown cell without hard coding the actual values in case they change. This is where string interpolation comes in! String interpolation is a common practice in software development and is used to insert a variable and make it appear as text like the information.
  • Surprisingly, Jupyter Notebooks do not support the inclusion of variables in Markdown Cells out of the box. If you still use Jupyter Notebooks there is a readily solution: the Python Markdown.
  • IPython
Notebook
  • Jupyter
  • QtConsole
  • JupyterLab
  • Jupyter Resources
  • Selected Reading

Markdown cell displays text which can be formatted using markdown language. In order to enter a text which should not be treated as code by Notebook server, it must be first converted as markdown cell either from cell menu or by using keyboard shortcut M while in command mode. The In[] prompt before cell disappears.

Header cell

A markdown cell can display header text of 6 sizes, similar to HTML headers. Start the text in markdown cell by # symbol. Use as many # symbols corresponding to level of header you want. It means single # will render biggest header line, and six # symbols renders header of smallest font size. The rendering will take place when you run the cell either from cell menu or run button of toolbar.

Following screenshot shows markdown cells in edit mode with headers of three different levels.

When cells are run, the output is as follows −

Note that Jupyter notebook markdown doesn’t support WYSWYG feature. The effect of formatting will be rendered only after the markdown cell is run.

Ordered Lists

To render a numbered list as is done by <ol> tag of HTML, the First item in the list should be numbered as 1. Subsequent items may be given any number. It will be rendered serially when the markdown cell is run. To show an indented list, press tab key and start first item in each sublist with 1.

If you give the following data for markdown −

It will display the following list −

Bullet lists

Each item in the list will display a solid circle if it starts with – symbol where as solid square symbol will be displayed if list starts with * symbol. The following example explains this feature −

The rendered markdown shows up as below −

Hyperlinks

Markdown text starting with http or https automatically renders hyperlink. To attach link to text, place text in square brackets [] and link in parentheses () optionally including hovering text. Following screenshot will explain this.

The rendered markdown appears as shown below −

Bold and Italics

To show a text in bold face, put it in between double underscores or two asterisks. To show in italics, put it between single underscores or single asterisks.

The result is as shown below −

Notebook

Images

To display image in a markdown cell, choose ‘Insert image’ option from Edit menu and browse to desired image file. The markdown cell shows its syntax as follows −

Image will be rendered on the notebook as shown below −

Variable

Table

In a markdown cell, a table can be constructed using (pipe symbol) and – (dash) to mark columns and rows. Note that the symbols need not be exactly aligned while typing. It should only take respective place of column borders and row border. Notebook will automatically resize according to content. A table is constructed as shown below −

The output table will be rendered as shown below −

Question or problem about Python programming:

I want to write a report for classes in Jupyter notebook. I’d like to count some stuff, generate some results and include them in markdown. Can I set the output of the cell to be interpreted as markdown?
I’d like such command: print ‘$phi$’ to generate phi symbol, just like in markdown.
In other words, I’d like to have a template made in markdown and insert the values generated by the program written in the notebook. Recalculating the notebook should generate new results and new markdown with those new values inserted. Is that possible with this software, or do I need to replace the values by myself?

How to solve the problem:

Solution 1:

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

Solution 2:

Jupyter Notebook Variable In Markdown

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:

Jupyter Notebook Markdown Code Block


Python Code in Markdown Cells
The variable a is {{a}}
You can also embed LateX: {{b}} in here!
Even images can be embedded: {{i}}

How To Do Markdown In Jupyter Notebook

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.

Jupyter Notebook Markdown Font Size

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

Hope this helps!