jgm / pandocfilters

A python module for writing pandoc filters, with a collection of examples
BSD 3-Clause "New" or "Revised" License
512 stars 111 forks source link

pandocfilter to include a ipython notebook #31

Closed kdheepak closed 8 years ago

kdheepak commented 8 years ago

I'm not sure if this is the best place to ask this question. I have a sample.md as shown below.

The following is a ipython notebook

{%ipython-notebook.ipynb%}

When a liquid-tag type syntax is found, I want to add notebook to AST. The end goal is to create a filter that allows a user to include a ipython notebook by using this syntax. I'm able to get the file_name from the sample.md file, and convert it to a markdown file. But I don't quite understand how to get from here to importing the markdown file / ipython notebook into the AST.

#!/usr/bin/env python

import os
import sys
from pandocfilters import toJSONFilter, Str, Para
import subprocess

def convert_notebook_to_markdown(file_name):
    subprocess.call(["jupyter", "nbconvert", "--to", "markdown", file_name]) 

def convert_markdown_to_para(file_name):
    # e.g. file_name is ipython-notebook.ipynb.
    # ipython-notebook.ipynb -> AST
    return(file_name)

def convert_notebook_to_para(file_name):
    convert_notebook_to_markdown(file_name)
    para = convert_markdown_to_para(file_name)
    return(para)

def notebook_convert(key, value, format, meta):
    if key == 'Para':
        sys.stderr.write("Printing Para " + str(value) + "\n")
        string_value = value[0]['c']
        if string_value[0:2] == "{%" and string_value[-2:] == "%}" and '.ipynb' in string_value:
            sys.stderr.write("Found a notebook!\n")
            file_name = string_value[2:-2]
            value[0]['c'] = convert_notebook_to_para(file_name)
        return Para(value)

if __name__ == "__main__":
    toJSONFilter(notebook_convert)

Does that make sense? Is this possible to do?

jgm commented 8 years ago

The library doesn't contain any special way to do this, so your best bet is to spawn a pandoc process in a shell. pandoc -s -f markdown -t json will convert to a json representation of the AST, which you can manually convert to a python object using json.loads.

+++ Dheepak Krishnamurthy [Dec 20 15 21:01 ]:

I'm not sure if this is the best place to ask this question. I have a sample.md as shown below. The following is a ipython notebook

{%ipython-notebook.ipynb%}

When a liquid-tag type syntax is found, I want to add notebook to AST. The end goal is to create a filter that allows a user to include a ipython notebook by using this syntax. I'm able to get the file_name from the sample.md file, and convert it to a markdown file. But I don't quite understand how to get from here to importing the markdown file / ipython notebook into the AST.

!/usr/bin/env python

import os import sys from pandocfilters import toJSONFilter, Str, Para import subprocess

def convert_notebook_to_markdown(file_name): subprocess.call(["jupyter", "nbconvert", "--to", "markdown", file_name])

def convert_markdown_to_para(file_name):

e.g. file_name is ipython-notebook.ipynb.

ipython-notebook.ipynb -> AST

return(file_name)

def convert_notebook_to_para(file_name): convert_notebook_to_markdown(file_name) para = convert_markdown_to_para(file_name) return(para)

def notebook_convert(key, value, format, meta): if key == 'Para': sys.stderr.write("Printing Para " + str(value) + "\n") string_value = value[0]['c'] if string_value[0:1] == "{%" and string_value[-2:-1] == "%}" and '.ipynb ' in string_value: file_name = string_value[2:-2] value[0]['c'] = convert_notebook_to_para(file_name) return Para(value)

if name == "main": toJSONFilter(notebook_convert)

Does that make sense? Is this possible to do?

— Reply to this email directly or [1]view it on GitHub.

References

  1. https://github.com/jgm/pandocfilters/issues/31
kdheepak commented 8 years ago

Thanks for your help @jgm. I was able to write a filter that allows a user to include a notebook. I've attached it here in case anyone else is interested in it.