gpoore / codebraid

Live code in Pandoc Markdown
BSD 3-Clause "New" or "Revised" License
367 stars 13 forks source link

Feature request: Force execution of all code cells without adding `.cb-run` and `.cb-nb` #59

Open mfhepp opened 1 year ago

mfhepp commented 1 year ago

Hi, it would be handy to have a command-line option that automatically adds either

or the respective behavior to all Python code block.

Motivation: I often create Python materials in Pandoc markdown and would want to produce a lecture note with the code snippets and their effects side-by-side. While I could manually add {.python .cb-run .cb-nb} to each code block, that seems like unnecessary redundancy. Before I start writing a custom Pandoc filter that adds .cb-run and .cb-nb to all code blocks in a Pandoc document, I wanted to check if there is another way with batteries supplied.

Implementation: I would use a CLI option like

mfhepp commented 1 year ago

Alternative implementation:

Support a YAML keyword runall, like so:

---
codebraid:
jupyter:
    kernel: python3
    timeout: 120
    runall: 
        - python
        - bash        
---
gpoore commented 1 year ago

To get something working immediately, I'd suggest running Pandoc with a filter on the original file to add the Codebraid classes, and then piping the Pandoc output through Codebraid.

Something like this should work for the filter:

function CodeBlock(elem)
    elem.classes:insert('python')
    elem.classes:insert('cb-nb')
    return elem
end

And then run with a command like this:

pandoc -f markdown -t markdown -L ./add_attr.lua ./attr.md | codebraid pandoc -f markdown -t markdown

I like the idea of adding a feature like this in the YAML. I'm planning to add YAML features for setting default code chunk options, and adding a feature like this would basically amount to the same thing except setting defaults for all code chunks, not just those marked with Codebraid classes. I'm currently working on updating some of my other open-source projects after focusing on Codebraid Preview earlier this year, so I may not be able to work on adding new features for a while.

mfhepp commented 1 year ago

Thanks a lot! Yes, I will be using a filter for the time being! Btw, the defaults files route is very powerful; I am developing a huge library of output formats mostly based on a hierarchy of defaults.yaml and meta.yaml files that reference their parent directory - very good for maintenance, because you minimize redundancy.