Skip to content

Quarto extension to split multilingual Quarto into language specific Jupyter notebooks

Notifications You must be signed in to change notification settings

JBGruber/quarto-targetlang

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Quarto split languages

What this is for

Developing notebooks on Google Colab sucks. However, when you need to teach a course in R or Python, Colab makes it a lot easier for the students to get started (and for the teachers who don’t have to run around cosplay an IT department). So I like to work in Quarto as long as possible and then use this addon to make 3 versions of my notebooks:

  1. A Python version with just the Python code.
  2. An R version with just the R code.
  3. An HTML version with both.

As Colab supports only one language at a time, this seems to make most sense.

Using the language filter script

Installing

quarto add jbgruber/quarto-targetlang

This will install the extension under the _extensions subdirectory. If you’re using version control, you will want to check in this directory.

Usage

Colab expects ipynb. This extension provides a special format which takes the argument target_lang in the YAML header, and removes all code chunks that do not fit this language:

format:
  targetlang-ipynb: 
    toc: false
    filters: 
      - langsplit.lua
  html: default
target_lang: "r"

You can render this from RStudio if you want. Or, what is even easier is to run it from the command line, using quarto-cli:

quarto render template.qmd --no-execute --to targetlang-ipynb -o template-py.ipynb --metadata target_lang:python
quarto render template.qmd --no-execute --to targetlang-ipynb -o template-r.ipynb --metadata target_lang:r
quarto render template.qmd --to html

Alternatively, there is also a quarto R package:

library(quarto)
library(quarto)
quarto_render(input = "template.qmd", output_format = "targetlang-ipynb", output_file = "template-py.ipynb", execute = FALSE, metadata = list(target_lang = "python"))
quarto_render(input = "template.qmd", output_format = "targetlang-ipynb", output_file = "template-r.ipynb", execute = FALSE, metadata = list(target_lang = "r"))
quarto_render(input = "template.qmd", output_format = "html")

Acknowledgment

I'm not very good with lua and couldn't wrap my head around the pandoc extension immediately either. So I'm grateful to the Stack Overflow users tarleb and Shafee who wrote most of the lua code, to be honest.

About

Quarto extension to split multilingual Quarto into language specific Jupyter notebooks

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages