fulaphex
fulaphex

Reputation: 3219

How to programmatically generate markdown output in Jupyter notebooks?

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?

Upvotes: 192

Views: 163702

Answers (10)

pxr687
pxr687

Reputation: 1

This can be done with the jupyprint package: https://pypi.org/project/jupyprint/

To print "live" variables in LaTeX/markdown, you can use something like the following code:

import jupyprint as jp

x=1
y=0
n=10

jp.jupyprint(f"$ {complex(x, y)}" + " ^ \\frac{" + f"{1}" + "}{" + f"{n}" + "} $")

Here is how the output looks.

There is a full demo here: https://github.com/pxr687/jupyprint/blob/main/jupyprint_demo.ipynb

Upvotes: 0

Arpan Saini
Arpan Saini

Reputation: 5181

IPython.display module.

   def sql_highlight_1(sql_query):
        # Method 1: Display using IPython Markdown
        from IPython.display import Markdown
        display(Markdown(f"```sql{sql_query}```"))


    sample_query = '''
select
            proc_dt as proc_dt_letter,
            trnsfrm_dt as trnsfrm_dt_letter,
            request_id as request_id_letter,
            letter_code as letter_code,
            CASE
                WHEN letter_stat = 'I' then 'inserted and online variables populated'
                WHEN letter_stat = 'B' then 'Batch variables populated'
                WHEN letter_stat = 'L' then 'letter format generated'
                WHEN letter_stat = 'E' then 'Error in batch variable population'
                WHEN letter_stat = 'X' then 'Extracted'
                WHEN letter_stat = 'C' then 'dispatch date uploaded and ready for archival'
                end as Letter_stat_desc,
            letter_stat as letter_stat,
            in_clob_value as input_letter,
            out_clob_value as output_letter,
            letter_layout,
            entity_id as entity_id_letter,
            entity_type as entity_type_letter,
            bnk_acct_nbr_tok bnk_acct_nbr_tok_letter,
            bod_date as bod_date_letter,
            comm_type as comm_type_letter,
            resp_req as resp_req_letter,
            upload_date as upload_date_letter,
            disp_date as dispatch_date_letter,
            bank_id as bank_id_letter,
            sol_id  as sol_id
        from Table_x
        where
                letter_code in ('1234','5678')
'''

# call high light function
sql_highlight_1(sample_query)

Output: enter image description here

Upvotes: 1

jans-code
jans-code

Reputation: 1

I've created a simple ipython kernel where all code cells are just giving back Markdown. Should be perfect for teaching/showing Markdown syntax. You can check it out here.

Upvotes: 0

feeeper
feeeper

Reputation: 3017

As an addition to Thomas's answer. Another easier way to render markdown markup is to use display_markdown function from IPython.display module:

from IPython.display import display_markdown

display_markdown('''## heading
- ordered
- list

The table below:

| id |value|
|:---|----:|
| a  |  1  |
| b  |  2  |
''', raw=True)

Output below:

enter image description here

Usage example could be found on Google Colab Notebook

Upvotes: 16

stevejpurves
stevejpurves

Reputation: 933

There is an interesting lab extension called jupyterlab-myst which will swap the standard markdown renderer in the notebook out for the mystjs renderer.

This means you can render more than standard commonmark markdown in markdown cells including being able to interpolate variable values directly into markdown. These can be simple variables, images, cell outputs and even ipywidgets.

This opens up a lot more possibilities on how you can interlace results from computation in code cells with markdown content in the notebooks. When a notebook is re-executed the interpolated values in the markdown will be updated.

There are other features that are helpful for report writing that could also help with the OP's use case.

Example of ipywidgets rendered in a markdown cell

Upvotes: 0

pplonski
pplonski

Reputation: 5839

One more option. There is an open-source framework for converting Jupyter notebooks to web apps. It is called Mercury. It has function called Markdown to display any string as Markdown. Here is a documentation.

Below is an example notebook that dynamically displays Markdown:

import mercury as mr
slider = mr.Slider(label="Favorite number", value=5)
name = "Piotr"
mr.Markdown(f"""# Hello {name}

## Your variable is {slider.value}
""")

notebook with markdown

Notebook and web app created with Mercury. It has dynamic Markdown: notebook and app with markdown

Upvotes: 0

nightcrawler
nightcrawler

Reputation: 327

from tabulate import tabulate
from IPython.display import Markdown
A2 = {
    'Variable':['Bundle Diameter','Shell Diameter','Shell Side Cross Flow area','Volumetric Flowrate','Shell Side Velocity'],
    'Result':[3.4, 34, 78.23, 1.0 ,  2.0],
    'Unit' : ['$in$', '$in$', '$ft^2$', '$ft^{3}s^{-1}$', '$fts^{-1}$']}
temp_html=tabulate(A2, headers='keys', tablefmt='html')
Markdown(temp_html.replace('<table>','<table style="width:50%">'))

.replace() usage will not break latex code & avoid column(s) overstrech. This way one can dynamically generate tables with Latex

Upvotes: 0

nms
nms

Reputation: 60

Another option is to use Rich for Markdown rendering and UnicodeIt for symbols. It has some limitations, as Rich uses CommonMark, which does not support tables, for example. Rich has other ways to render tables though; this is detailed in the documentation.

Here is an example:

from rich.markdown import Markdown
import unicodeit

alpha = unicodeit.replace('\\alpha')
epsilon = unicodeit.replace('\\epsilon')
phi = unicodeit.replace('\\phi')

MARKDOWN = f"""
# This is an h1

Rich can do a pretty *decent* job of rendering markdown.

1. This is a list item
2. This is another list item

## This is an h2

List of **symbols**:

- alpha: {alpha}
- epsilon: {epsilon}
- phi: {phi}

This is a `code` snippet:

```py
# Hello world
print('Hello world')
```

This is a blockquote:

> Rich uses [CommonMark](https://commonmark.org/) to parse Markdown.

---

### This is an h3

See [Rich](https://github.com/Textualize/rich) and [UnicodeIt](https://github.com/svenkreiss/unicodeit) for more information.
"""

Markdown(MARKDOWN)

... which produces the following output:

Rich + UnicodeIt output

Upvotes: 2

Honeybear
Honeybear

Reputation: 3138

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.

Upvotes: 40

Thomas K
Thomas K

Reputation: 40330

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'))

Upvotes: 318

Related Questions