Can I include text from other files in quarto?

quarto
html
_playground
Published

June 11, 2023

Just playing around with embeding quarto again.

<embed src="data/test.txt">
<object type="text/plain" data="file.txt"></object>

Doesn’t look quite right.

quarto include function

"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."

“Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.”

This works. But how can I get formatted text? I would like to include code from files outside the main file, for ease of editing and modularity.

<script src=" https://cdn.jsdelivr.net/npm/prismjs@1.29.0/prism.min.js "></script>
<script src="https://cdn.jsdelivr.net/npm/prismjs@v1.x/plugins/autoloader/prism-autoloader.min.js"></script>

<link href=" https://cdn.jsdelivr.net/npm/prismjs@1.29.0/themes/prism.min.css " rel="stylesheet">

<pre><code class="language-python">import numpy as np</code></pre>
import numpy as np

Bit weird, but it works. Now how can I include external content from external files with this?

data/test.txt

Since quarto can run python, and you can force the output of that to render to markdown with a few options:

#| output: asis
#| echo: true

backtick = "`"
a = ""
with open('data/python.py', 'r') as f:
    file_contents = f.read()
    a = file_contents

print(f'''

{backtick * 3}{{.python filename="data/python.py"}}
{a}
{backtick * 3}
''')

Of course, the proper quarto syntax is:

```{python}
#| options here
# python code here
```
data/python.py
# A function to check if a number is prime
def is_prime(n):
  # If n is less than 2, it is not prime
  if n < 2:
    return False
  # Loop from 2 to the square root of n
  for i in range(2, int(n**0.5) + 1):
    # If n is divisible by i, it is not prime
    if n % i == 0:
      return False
  # If no divisor is found, it is prime
  return True

# Test the function with some numbers
print(is_prime(2)) # True
print(is_prime(3)) # True
print(is_prime(4)) # False
print(is_prime(5)) # True
print(is_prime(6)) # False

It works! Perhaps an awkward way of doing this, but it works. I can probably even get code from remote repos, since it’s all python. There are other ways to do this, but I didn’t want to do have to rely on any extensions or external dependencies.