.qmd and python jupyter blocks

description
Published

February 20, 2024

What are qmd files?

.qmd files are files special to quarto. The short version of it is that quarto renders these files, which are essentially markdown files, to jupyter notebooks, where it then executes and renders any python code.

Examples

print("Hello World!")

print(3 + 2)
Hello World!
5

Quarto is designed for data science, and this means having executable code blocks that do something. For example, Quarto can use matplotlib in python to plot things.

Where things get interesting is the combination of three factors:

  • The output of these code blocks can be done as plain text, rather than a code block, by putting #| output: asis at the beginning of the code block.
  • The plain text output by these code blocks, is done before the markdown is rendered to other formats.
  • It’s possible to hide the code blocks but not the output, by putting #| echo: false at the beginning of the code block itself

What this means, is that python can essentially act as a formatting engine, for generating dynamic content. Even things like using.

#| output: asis
#| echo: false

def make_bullets(list):
  bulletlist = f''''''
  for i in list:
    bulletlist = bulletlist + "* "
    bulletlist = bulletlist + i
    bulletlist = bulletlist + "\n"
  print(bulletlist)

sample_list = [
  "Item 1",
  "Item 2",
  "Item 3"
]

make_bullets(sample_list)

Which renders to:

  • Item 1
  • Item 2
  • Item 3

It should be noted that I am actually using two code blocks here, one which does not execute, and does not hide itself, and another which executes and hides itself.

You can get even more dynamic content, if you use something like python requests to pull it as you render the site. Again, it is a bit hacky since backticks would render into a code block, so here I multiply one backticks times 3 to get a string containing 3 backticks, but it does work.

#| output: asis
#| echo: false

import requests
backticks = "`" * 3

print(f'''

{backticks}{{.nix .code-overflow-wrap filename=home.nix}}
{requests.get('https://raw.githubusercontent.com/ErikMcClure/bad-licenses/3c26bef3028fa8470308c493af7d7eae152a19ba/overwatch').text}
{backticks}

''')
home.nix
                     Overwatch License Revision 2
                          (c) Author, year


Permission is hereby granted, free of charge, for anyone to use, distribute, or
sell the compiled binaries, source code, and documentation (the "Software")
without attribution.

Permission to modify the Software is only granted to those that have a higher
competitive matchmaking rank than the copyright holder in Overwatch 2
(Blizzard, 2022).

The Software is provided in the hope that some will find it useful, but the
Software comes under NO WARRANTY, EXPRESS OR IMPLIED, and the authors of the
Software are NOT LIABLE IN THE EVENT OF LOSSES, DAMAGES OR MISUSE relating to
the Software.