Skip to main content
# genhtml-markdown
[Python-Markdown](http://pythonhosted.org/Markdown/) plugin allowing to build HTML from inline python source.

Direct applications includes [charts and other plots](https://plot.ly/python/) in markdown documents, and tooling for [blogging](https://blog.getpelican.com/).

pip install genhtml-markdown

See the [compiled examples and their sources](examples/) for an introductory tour, the [Makefile](Makefile) for the process, or look at next section:


## Basic example with plotly
Let's take an [offline scatter plot example](https://plot.ly/python/getting-started/#initialization-for-offline-plotting), modified to print the generated HTML :

```python
import plotly.offline
import plotly.graph_objs as go

data = go.Scatter(x=[1, 2, 3, 4], y=[4, 3, 2, 1])
layout = go.Layout(title="hello world")

figure = go.Figure(data=[data], layout=layout)
print(offline.plot(figure, output_type='div'))
```

You could want to include it in your article. With genhtml-markdown extension, it's easy:

```genhtml
import plotly.offline
import plotly.graph_objs as go

data = go.Scatter(x=[1, 2, 3, 4], y=[4, 3, 2, 1])
layout = go.Layout(title="hello world")

figure = go.Figure(data=[data], layout=layout)
print(offline.plot(figure, output_type='div'))
```

You think it's verbose ? I do too. That's the reason we have *headers* and *footers*, patches of python that will be put respectively *before* and *after* our specific code. By default, the header is full of imports, including the plotly ones. And the two last lines are provided by the *plot* footer. So, here is our final code:

```genhtml footer=plot
data = go.Scatter(x=[1, 2, 3, 4], y=[4, 3, 2, 1])
layout = go.Layout(title="hello world")
```

Imports and offline plotting boilerplate code will be added by headers/footers, the total code will be ran, and finally the output will be included in place as an interactive plot/chart.


## Features

### Ready-to-use headers and footers
You can see the full list of [headers](headers/) and [footers](footers/) in their respective directories. You can also pass `headers_dir` and `footers_dir` [parameters for the extensions](https://python-markdown.github.io/cli/#using-extensions) in order to provide your own !

For instance, with the parameter `-c config.json` added to `python -m markdown` call, you can feed markdown with the following parameters:

```json
{
"genhtml": {
"headers_dir": "alt-headers"
}
}
```

Indicating that genhtml will also look in the `alt-headers/` directory for headers.
Options can also be set when calling [markdown module programatically](https://python-markdown.github.io/extensions/api/#configsettings) with something like `markdown.Markdown(extensions=[GenHTMLMarkdownExtension(headers_dir='~/my-headers-dir'])`.

Finally, note that the [default header](headers/default.py) provides a lot of imports.

### Generate and show raw images
As shown in [images example](examples/images.mkd),
it is quite easy to build and show an image inline :

```genhtml format=png footer=png-image
from PIL import Image
image = Image.new('RGB', (60, 30), color='green')
```

The `format=png` options tells that the printed data is (base64-encoded) raw png data,
and the [*png-image* footer](genhtml/footers/png-image.py) reads something like:

```python
import io
import base64
with io.BytesIO() as output:
image.save(output, format='png')
print(base64.b64encode(output.getvalue()).decode(), end='')
```

Other formats are `jpg` and `svg`, allowing you to [bring gizeh to your markdown](https://github.com/Zulko/gizeh).


### Graphs
Using the previous feature, it becomes possible to draw graphs from their networkx definition, as shown in [the related example](examples/networkx_and_dot.mkd).

Using the *dot-png* footer, you can just build your graph and print it in no time:

```genhtml format=png footer=dot-png
graph = nx.fast_gnp_random_graph(10, 0.5)
# draw the first one in beige
graph.nodes[1]['style'] = 'filled'
graph.nodes[1]['fillcolor'] = 'beige'
```

### Show sources, and other code manipulations
See the related [example](examples/arbitrary-python.mkd).

### Nested code generation
Is fully supported. See [pyception example](examples/pyception.mkd).


### Use static data
You have in the code access to active directory.
For instance, if using [pelican](https://blog.getpelican.com/), you can read all your blog articles in few lines:

```genhtml
import glob
print('Follows a list of published articles:')
for article in glob.glob('content/articles/*.mkd'):
if any(line.strip() == 'status: published' for line in open(article)):
print(f'- {article}')
```

Following this logic, if you are using recurrent CSV data, you could put it in a dedicated directory,
and access it easily from all your integrated python code.

### Global environment
An environment is shared among codes in the same document.
As shown in [related example](examples/env-management.mkd), you can access it with the flag `global-env=true`.

## Other features
- headers/footers for [biseau](https://gitlab.inria.fr/lbourneu/biseau), allowing [ASP](https://lucas.bourneuf.net/blog/asp-tuto.html) in markdown to draw graphs.

## Incoming features
- access to all markdown article from python (allowing to access the article itself)

Metadata

Release files for genhtml-markdown 1.0.9

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for genhtml-markdown 1.0.9
File Size Uploaded
genhtml-markdown-1.0.9.tar.gz 8.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for genhtml-markdown 1.0.9
File Interpreter ABI Platform
genhtml_markdown-1.0.9-py3-none-any.whl Python 3 none any Details

Total release size: 19.3 kB

Release files / genhtml-markdown-1.0.9.tar.gz

Download URL genhtml-markdown-1.0.9.tar.gz
Size 8.6 kB
Tags Source
SHA-256 checksum
How to use checksums
48f52c7bb4e2907a7546f4484520049d6dacd283ddd14660a6d247ff1898d9e7
BLAKE2b-256 checksum
How to use checksums
147d8ea3cef809c61625b76b65bc58ad7bc5f803b39b23465f7360e978ce8295
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.1 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.6.6

Release files / genhtml_markdown-1.0.9-py3-none-any.whl

Download URL genhtml_markdown-1.0.9-py3-none-any.whl
Size 10.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
097b7231260417b3588926f33782f6495d1c0e23e0545d6c784f404dbde1e04d
BLAKE2b-256 checksum
How to use checksums
94e2e407935376fc384f648cd7c1052a6594e06258d805ddd98f12a6a7b2a070
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.1 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.6.6

Release history Release notifications | RSS feed

This release

1.0.9 This release

2 release files

1.0.8

2 release files

1.0.7

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page