Convert notebook to a standalone markdown

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.

A nice option to share an entire previously run notebook in static form is to post the notebook as a Github gist and link to it.
You can then even paste the gist URL into nbviewer 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.

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.

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.

1 Like

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.

Are you sure discourse allows data: URIs? See Danie’s B answer here for discussion related to that issue. If it allows it you can parse out the base64 encoded data from a saved notebook .json file.

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.

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

1 Like

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.

1 Like