Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/jbgruber/quarto-targetlang

Quarto extension to split multilingual Quarto into language specific Jupyter notebooks
https://github.com/jbgruber/quarto-targetlang

Last synced: 3 months ago
JSON representation

Quarto extension to split multilingual Quarto into language specific Jupyter notebooks

Awesome Lists containing this project

README

        

# Quarto split languages

- [What this is for](#what-this-is-for)
- [Using the language filter script](#using-the-language-filter-script)
- [Installing](#installing)
- [Usage](#usage)

## 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

``` bash
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:

``` yaml
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:

``` bash
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:

``` r
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](https://stackoverflow.com/users/2425163/tarleb) and [Shafee](https://stackoverflow.com/users/10858321/shafee) who wrote most of the lua code, to be honest.