Embed a file that isn’t a note, ![[sieve.py]], and it’s shown from the file itself: change the code and the page changes with it.

A code file

![[sieve.py]] is highlighted and titled with its name. Point at it, and its bar shows what you can do with it: wrap its lines, open the raw file, copy the code, or see it on the whole screen. It’s the code that drew the sieve on margin. Its first line is wider than the page: point at it, and the code reaches into the side content to show the rest.

sieve.py
"""The sieve of Eratosthenes: the primes below n, by crossing out multiples."""
 
 
def sieve(n: int) -> list[int]:
    is_prime = [True] * n
    is_prime[0] = is_prime[1] = False
    for p in range(2, int(n**0.5) + 1):
        if is_prime[p]:
            # every multiple of p from p² up is composite
            for multiple in range(p * p, n, p):
                is_prime[multiple] = False
    return [i for i, prime in enumerate(is_prime) if prime]
 
 
if __name__ == "__main__":
    print(sieve(100))

Some of its lines

![[sieve.py#L7-L11]] shows just those lines, numbered from where they start:

the sieve itself
    for p in range(2, int(n**0.5) + 1):
        if is_prime[p]:
            # every multiple of p from p² up is composite
            for multiple in range(p * p, n, p):
                is_prime[multiple] = False

A code block

Any code block has the same controls, inside it. A block with a title, ```ts title="primes.ts", has them in its bar; one without has them in its top right corner.

primes.ts
// the primes up to n, by crossing out the multiples of each one in turn (Eratosthenes, about 240 BC)
export function primes(n: number): number[] {
  const crossed = new Uint8Array(n + 1)
  const found: number[] = []
  for (let p = 2; p <= n; p++) {
    if (crossed[p]) continue
    found.push(p)
    for (let m = p * p; m <= n; m += p) crossed[m] = 1
  }
  return found
}
 
// Euclid's numbers: one more than the product of the first k primes
export const euclid = (k: number) => primes(100).slice(0, k).reduce((a, p) => a * p, 1) + 1
basalt dev my-garden

The same code in two languages can take turns, in tabs.

A table

A csv file, ![[euclid.csv]], becomes a table. These are the Euclid numbers from the proof on margin, and the first six are prime:

nprimes multipliedEuclid numberfactors
123prime
22 to 37prime
32 to 531prime
42 to 7211prime
52 to 112311prime
62 to 133003159 × 509
72 to 1751051119 × 97 × 277
82 to 199699691347 × 27953
92 to 23223092871317 × 703763
102 to 296469693231331 × 571 × 34231

A file that isn’t there

A name that matches no file says so, instead of breaking the page:

couldn't find nope.py to embed

---
title: Code and data
description: "Code, a few of its lines, or a csv as a table, shown from the file itself."
aliases: [files, examples/markdown/files]
---
 
Embed a file that isn't a note, `![[sieve.py]]`, and it's shown from the file itself: change the code and the page changes with it.
 
## A code file
 
`![[sieve.py]]` is highlighted and titled with its name. Point at it, and its bar shows what you can do with it: wrap its lines, open the raw file, copy the code, or see it on the whole screen. It's the code that drew the sieve on [[margin]]. Its first line is wider than the page: point at it, and the code reaches into the side content to show the rest.
 
![[sieve.py]]
 
## Some of its lines
 
`![[sieve.py#L7-L11]]` shows just those lines, numbered from where they start:
 
![[sieve.py#L7-L11|the sieve itself]]
 
## A code block
 
Any code block has the same controls, inside it. A block with a title, ```` ```ts title="primes.ts" ````, has them in its bar; one without has them in its top right corner.
 
```ts title="primes.ts"
// the primes up to n, by crossing out the multiples of each one in turn (Eratosthenes, about 240 BC)
export function primes(n: number): number[] {
  const crossed = new Uint8Array(n + 1)
  const found: number[] = []
  for (let p = 2; p <= n; p++) {
    if (crossed[p]) continue
    found.push(p)
    for (let m = p * p; m <= n; m += p) crossed[m] = 1
  }
  return found
}
 
// Euclid's numbers: one more than the product of the first k primes
export const euclid = (k: number) => primes(100).slice(0, k).reduce((a, p) => a * p, 1) + 1
```
 
```sh
basalt dev my-garden
```
 
The same code in two languages can take turns, in [[text#tabs|tabs]].
 
## A table
 
A csv file, `![[euclid.csv]]`, becomes a table. These are the Euclid numbers from the proof on [[margin]], and the first six are prime:
 
![[euclid.csv]]
 
## A file that isn't there
 
A name that matches no file says so, instead of breaking the page:
 
![[nope.py]]