# Convert notebook to a standalone markdown

**URL:** <https://discourse.jupyter.org/t/convert-notebook-to-a-standalone-markdown/5363>\
**Category:** nbconvert\
**Created:** [July 24, 2020, 6:15pm UTC](https://discourse.jupyter.org/t/convert-notebook-to-a-standalone-markdown/5363 "2020-07-24T18:15:18Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![areebbeigh](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/areebbeigh/32/2450_2.png) [@areebbeigh](https://discourse.jupyter.org/u/areebbeigh)\
**Post date:** [July 24, 2020, 6:15pm UTC](https://discourse.jupyter.org/t/convert-notebook-to-a-standalone-markdown/5363/1 "2020-07-24T18:15:18Z")

</div>

Hi. Can I use `nbconvert --to markdown` to write all the generated images in the notebook as their data URIs instead of writing them to disk?

I want to be able to share the notebook on discourse forums.

---

<div class="post-metadata">

**Author:** ![fomightez](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/fomightez/32/495_2.png) [@fomightez](https://discourse.jupyter.org/u/fomightez)\
**Post date:** [July 24, 2020, 8:04pm UTC](https://discourse.jupyter.org/t/convert-notebook-to-a-standalone-markdown/5363/2 "2020-07-24T20:04:14Z")

</div>

A nice option to share an entire previously run notebook in static form is to post the notebook as a [Github gist](https://gist.github.com/) and link to it.  
You can then even paste the gist URL into [nbviewer](https://nbviewer.jupyter.org/) and use the link it generates in the resulting view, if you prefer those interested get directed to the cleaner interface / nicer rendering.

Additionally, by already having it as a Gist (or in Github repo), once a user is viewing it via nbviewer, that user can click on the MyBinder logo (three rings interwoven) in the upper right & open that as an active notebook via MyBinder. Then if they know what they are doing, the interested user can be working with your notebook without needing to download anything. I say ‘if they know what they are doing’ because they may need to install packages via `%pip install` or `%conda install` if you didn’t go the extra mile to include Binder-compatible configuration files.

---

<div class="post-metadata">

**Author:** ![areebbeigh](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/areebbeigh/32/2450_2.png) [@areebbeigh](https://discourse.jupyter.org/u/areebbeigh)\
**Post date:** [July 24, 2020, 8:07pm UTC](https://discourse.jupyter.org/t/convert-notebook-to-a-standalone-markdown/5363/3 "2020-07-24T20:07:07Z")

</div>

Yes, they’re options I’ve considered. However, I want to be able to display the run notebook statically without redirecting the user to a third party.

---

<div class="post-metadata">

**Author:** ![MSeal](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/mseal/32/253_2.png) [@MSeal](https://discourse.jupyter.org/u/MSeal)\
**Post date:** [July 24, 2020, 8:11pm UTC](https://discourse.jupyter.org/t/convert-notebook-to-a-standalone-markdown/5363/4 "2020-07-24T20:11:18Z")

</div>

There was a suggested feature around supporting outputing to gists in nbconvert. This was for code snippets but I could see a feature being added to push outputs in a similar fashion. Today I’m not aware of a tooling setup that will do this for you without a third-party redirect.

---

<div class="post-metadata">

**Author:** ![areebbeigh](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/areebbeigh/32/2450_2.png) [@areebbeigh](https://discourse.jupyter.org/u/areebbeigh)\
**Post date:** [July 24, 2020, 8:22pm UTC](https://discourse.jupyter.org/t/convert-notebook-to-a-standalone-markdown/5363/5 "2020-07-24T20:22:39Z")

</div>

Alright. In that case, can I get a clue for where I should look to implement something like this myself?

Right now, my concern is only converting images to data URIs to make the resulting markdown output standalone.

---

<div class="post-metadata">

**Author:** ![fomightez](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/fomightez/32/495_2.png) [@fomightez](https://discourse.jupyter.org/u/fomightez)\
**Post date:** [July 24, 2020, 8:29pm UTC](https://discourse.jupyter.org/t/convert-notebook-to-a-standalone-markdown/5363/6 "2020-07-24T20:29:48Z")

</div>

Are you sure discourse allows `data:` URIs? See Danie’s B answer [here](https://superuser.com/questions/1199393/is-it-possible-to-directly-embed-an-image-into-a-markdown-document) for discussion related to that issue. If it allows it you can parse out the base64 encoded data from a saved notebook .json file.

---

<div class="post-metadata">

**Author:** ![areebbeigh](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/areebbeigh/32/2450_2.png) [@areebbeigh](https://discourse.jupyter.org/u/areebbeigh)\
**Post date:** [July 24, 2020, 8:49pm UTC](https://discourse.jupyter.org/t/convert-notebook-to-a-standalone-markdown/5363/7 "2020-07-24T20:49:03Z")

</div>

Okay. I think I have what I need to move ahead.

Still curious about how GitHub gists or .ipynb files render images for notebooks in markdown though. Or maybe they convert to html with data URIs instead of markdown.

---

<div class="post-metadata">

**Author:** ![MSeal](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/mseal/32/253_2.png) [@MSeal](https://discourse.jupyter.org/u/MSeal)\
**Post date:** [July 24, 2020, 9:08pm UTC](https://discourse.jupyter.org/t/convert-notebook-to-a-standalone-markdown/5363/8 "2020-07-24T21:08:57Z")

</div>

@areebbeigh If you run nbconvert with `--ExtractOutputPreprocessor.enabled=False` it will embed the images inline in the markdown output fyi.

---

<div class="post-metadata">

**Author:** ![MSeal](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/mseal/32/253_2.png) [@MSeal](https://discourse.jupyter.org/u/MSeal)\
**Post date:** [July 24, 2020, 9:11pm UTC](https://discourse.jupyter.org/t/convert-notebook-to-a-standalone-markdown/5363/9 "2020-07-24T21:11:40Z")

</div>

If you wanted to look more into gists, you’ll need too look at the `markdown/index.md.j2` file in how it handles images. You’d need to add a `markdown2gist` filter to the `filters/markdown.py` file and then test that function to post a gist and return the markdown linking to it.

---

<div class="post-metadata">

**Author:** ![XIVN1987](https://avatars.discourse-cdn.com/v4/letter/x/e495f1/32.png) [@XIVN1987](https://discourse.jupyter.org/u/XIVN1987)\
**Post date:** [December 30, 2020, 1:33pm UTC](https://discourse.jupyter.org/t/convert-notebook-to-a-standalone-markdown/5363/10 "2020-12-30T13:33:19Z")

</div>

i have same need, and found by replacing  
![png](output\_3\_0.png)  
to  
\<img src=“data:image/png;base64,iVBO…”\>

image is successfully embedded to markdown file

i will write a python script to do this replace.

---

<div class="post-metadata">

**Author:** ![XIVN1987](https://avatars.discourse-cdn.com/v4/letter/x/e495f1/32.png) [@XIVN1987](https://discourse.jupyter.org/u/XIVN1987)\
**Post date:** [December 30, 2020, 3:02pm UTC](https://discourse.jupyter.org/t/convert-notebook-to-a-standalone-markdown/5363/11 "2020-12-30T15:02:37Z")

</div>

convert script:

```
#! python3
import os
import re
import binascii

srcfil = r'C:\Users\WMX\Desktop\pylab\pylab.md'

srcdir = os.path.dirname(srcfil)
dstfil = os.path.join(srcdir, os.path.basename(srcfil).replace('.md', '_emb.md'))

img_base64 = {}
for root, dirs, files in os.walk(srcdir):
    for fname in files:
        if fname.endswith('.png'):
            img_data = open(os.path.join(root, fname), 'rb').read()
            img_base64[fname] = f'<img src="data:image/png;base64,{binascii.b2a_base64(img_data, newline=False).decode("latin-1")}">'

content = open(srcfil, 'r', encoding='utf-8').read()
for img in img_base64:
    content = re.sub(f'\n!\[png\]\({img}\)\n', f'\n{img_base64[img]}\n', content)

open(dstfil, 'w', encoding='utf-8').write(content)
```
