Codebraid: Live Code in Pandoc Markdown

Codebraid: Live Code in Pandoc Markdown

54 PROC. OF THE 18th PYTHON IN SCIENCE CONF. (SCIPY 2019) Codebraid: Live Code in Pandoc Markdown Geoffrey M. Poore‡∗ F Abstract—Codebraid executes code blocks and inline code in Pandoc Mark- Codebraid includes a built-in code execution system. It can down documents as part of the document build process. Code can be exe- also use Jupyter kernels [KRKP+16] to execute code. The first cuted with a built-in system or Jupyter kernels. Either way, a single document code block that is executed with a given language can specify a can involve multiple programming languages, as well as multiple independent kernel. In the example below, the “.cb.nb” tells Codebraid to sessions or processes per language. Because Codebraid only uses standard run the code and provide a “notebook” display that shows both Pandoc Markdown syntax, Pandoc handles all Markdown parsing and format code and output, while “jupyter_kernel” specifies a kernel. conversions. In the final output document produced by Pandoc, a code chunk can be replaced by a display of any combination of its original Markdown source, ```{.python .cb.nb jupyter_kernel=python3} its code, the stdout or stderr resulting from execution, or rich output in the case from sympy import * of Jupyter kernels. There is also support for programmatically copying code or init_printing() output to other parts of a document. x = Symbol('x') integral = Integral(E**(-x**2), (x, -oo, oo)) Index Terms—reproducibility, dynamic report generation, literate programming, display(integral) integral.doit() Python, Pandoc, Project Jupyter ``` Introduction Because a Jupyter kernel was used to run the code, the result includes rich output in the form of rendered LaTeX math, just as Scientific and technical documents are increasingly written with it would in a Jupyter notebook: software that allows a mixture of text and executable code, such + as the Jupyter Notebook [KRKP 16], knitr [YX15], and Org- from sympy import * mode Babel [SD11], [SDDD12]. Writing with such tools can init_printing() enhance reproducibility, simplify code documentation, and aid in x= Symbol('x') integral= Integral(E **(-x**2), (x,-oo, oo)) automating reports. display(integral) This paper introduces Codebraid, which allows executable integral.doit() code within Pandoc Markdown documents [JG19], [JM19]. Code- Z ¥ 2 braid is developed at https://github.com/gpoore/codebraid and e−x dx is available from the Python Package Index (PyPI). It allows −¥ p Markdown code blocks like the one below to be executed during p the document build process. In this case, the “.cb.run” tells Codebraid to run the code and include the output. The next section provides an example of the document build ```{.python .cb.run} process with Codebraid. This is followed by an overview of Code- print("Running code within *Markdown!*") braid features and capabilities. Finally, the Comparison considers ``` Codebraid in the context of knitr, Pweave, Org-mode Babel, and The final document contains the code’s output, interpreted as if it the Jupyter Notebook. had been entered directly in the original Markdown source: Running code within Markdown! Building a simple Codebraid document A document using Codebraid can be converted from Mark- A simple Pandoc Markdown document that runs code with Code- down into any of the many formats supported by Pandoc, such as braid is shown below. HTML, Microsoft Word, LaTeX, and PDF. Codebraid delegates all Markdown parsing and format conversions to Pandoc, so it does ```{.python .cb.run name=part1} not introduce any special restrictions on what is possible with a var1 = "Hello from *Python!*" var2 = f"Here is some math: $2^8={2**8}$." Pandoc Markdown document. This close integration with Pandoc ``` also means that Codebraid can be extended in the future to work with additional document formats beyond Markdown. ```{.python .cb.run name=part2} print(var1) print(var2) * Corresponding author: [email protected] ‡ Union University ``` Copyright © 2019 Geoffrey M. Poore. This is an open-access article distributed If this were a normal Pandoc document, converting it into a under the terms of the Creative Commons Attribution License, which permits format such as reStructuredText could be accomplished by running unrestricted use, distribution, and reproduction in any medium, provided the original author and source are credited. pandoc --from markdown --to rst file.md CODEBRAID: LIVE CODE IN PANDOC MARKDOWN 55 Using Codebraid to execute code as part of the document con- in Markdown, then converted to reStructuredText via Codebraid version process is simply a matter of replacing pandoc with with Pandoc. Finally, the reStructuredText was converted through codebraid pandoc: LaTeX to PDF via Docutils [DG16]. The two code blocks in the codebraid pandoc --from markdown --to rst file.md example were only entered in the original Markdown source of this paper a single time, and Codebraid only executed them a The codebraid executable is available from the Python Pack- single time. However, with Codebraid’s copy-paste capabilities, it age Index (PyPI); development is at https://github.com/gpoore/ was possible to display the code and output at other locations in codebraid. By default, code is executed with Codebraid’s built-in the document programmatically. code execution system. This can easily be swapped for a Jupyter The rendered output of the two code blocks is shown at the kernel, as shown in the Introduction and discussed in greater detail very end of the earlier section. This is where the code blocks were in Jupyter kernels. actually entered in the original Markdown source of this paper, When this codebraid pandoc command is executed, the and where they were executed. original Markdown shown above is converted into Codebraid- Recall that both blocks were given names, part1 and part2. processed Markdown: This enables any combination of their Markdown source, code, Hello from *Python!* stdout, and stderr to be inserted elsewhere in the document. At Here is some math: $2^8=256$. the beginning of the earlier section, the Markdown source for the blocks was shown. This was accomplished via This processed Markdown is then converted into the final reStruc- turedText, rendering as ```{.cb.paste copy=part1+part2 show=copied_markup} ``` Hello from Python! Here is some math: 28 = 256. The cb.paste command inserts copied data from one or more cb.run By default, the output of code executed with is in- code chunks that are specified with the copy keyword. Mean- terpreted as Markdown. It is possible to show the output verbatim while, the show keyword controls what is displayed. In this case, instead, as discussed later. the Markdown source of the copied code chunks was shown. Since In this example, the code is simple enough that it could be the cb.paste command is copying content from elsewhere, it executed every time the document is built, but that will often not is used with an empty code block. Alternatively, a single empty be the case. By default, Codebraid caches all code output, and line or a single line containing an underscore is allowed as a code is only re-executed when it is modified. This can be changed placeholder. by building with the flag --no-cache. Toward the end of the earlier section, the verbatim output of the Codebraid-processed Markdown was displayed. This was Pandoc code attribute syntax inserted in a similar manner: Pandoc Markdown defines an attribute syntax for inline code and code blocks. Codebraid uses this to designate which code blocks ```{.cb.paste copy=part1+part2 show=stdout:verbatim} ``` should be executed and provide options. Code attributes have the general form The default format of stdout is verbatim, but this was {#id .class1 .class2 key1=value1 key2=value2} specified just to be explicit. The other option is raw (interpreted as Markdown). If code with these attributes were converted into HTML, #id be- Of course, all Markdown shown in the current section was comes an HTML id for the code, anything with the form .class itself inserted programmatically using cb.paste to copy from specifies classes, and space-separated key-value pairs provide the earlier section. However, to prevent infinite recursion, the next additional attributes. Although key-value pairs can be quoted with section is not devoted to explaining how this was accomplished. double quotation marks, Pandoc allows most characters except the space and equals sign unquoted. Other output formats such as LaTeX use attributes in a largely equivalent manner. Other Codebraid commands Pandoc uses the first class to determine the language name The commands cb.run and cb.paste have already been for syntax highlighting, hence the .python in the example in the introduced. There are three additional commands. last section. Codebraid uses the second class to specify a command The cb.code command simply displays code, like normal for processing the code. All Codebraid commands are under a cb inline code or a code block. It primarily exists so that normal namespace to prevent unintentional collisions with normal Pandoc code can be named, and then accessed later. cb.paste could be attributes. In the example, cb.run indicates that code should be used to insert the code elsewhere, perhaps combined with code run, stdout should be included and interpreted as Markdown, and from other sources via something like copy=code1+code2. It stderr should be displayed in the event of errors. If a Jupyter kernel would also be possible to run the code elsewhere: were in use, rich output such as plots would also be included. ```{.cb.run copy=code1+code2} Finally, the name keyword is used to assign a unique name to each ``` piece of code. This allows the code to be referenced elsewhere in a document to insert any combination of its Markdown source, When copy is used with cb.run, or another command that code, stdout, stderr, and rich output (for Jupyter kernels).

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    8 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us