Skip to content

0.19.0

Compare
Choose a tag to compare
@nalgeon nalgeon released this 15 Mar 05:56
· 29 commits to main since this release

This release introduces three output-related features:

  • Auto-tail output.
  • Manual tail.
  • Empty placeholder.

⚠️ All three features are experimental and may be removed in future releases.

Auto-tail output

Suppose you have two code snippets where the second depends on the first:

a = 42
print("a =", a)
<codapi-snippet id="s-1" sandbox="python" editor="basic">
</codapi-snippet>
b = a * 2
print("b =", b)
<codapi-snippet sandbox="python" editor="basic" depends-on="s-1">
</codapi-snippet>

When you run the second snippet, it outputs the following:

a = 42
b = 84

It's probably not what you expect, because the code in the second snippet only prints b. But that's how snippet dependencies worked until now.

Now you can force the snippet to ignore the output of its dependencies and print only its own output. Use the output-tail attribute to do this:

<codapi-snippet sandbox="python" editor="basic" depends-on="s-1" output-tail>
</codapi-snippet>

Now it will only print b = 84.

Auto-tail only works for some languages (namely JS, Lua, PHP, Python, R, Ruby, TypeScript, Shell, and SQL). The language is inferred from the sandbox attribute by default, but you can set it manually with the syntax attribute.

For other languages, you'll need to use the manual tail (see below).

Manual tail

You can use output-tail to manually suppress some output and only show the tail. This works for both snippets with dependencies and regular snippets.

Suppose you use a third-party package to do some work and display the result:

import chatty

res = chatty.work()
print("res =", res)

For reasons beyond your control, chatty.work() prints a lot of stuff to stdout. You'd like to hide it from the reader and only show the result. To do this, add the output-tail attribute and print the horizontal rule separator (---) to mark the "tail":

import chatty

res = chatty.work()
print("---")
print("res =", res)
<codapi-snippet sandbox="python" editor="basic" output-tail>
</codapi-snippet>

This snippet ignores everything before and including --- and only shows res = ....

Empty output placeholder

Suppose you have a snippet that doesn't print anything:

res = work()
<codapi-snippet sandbox="python" editor="basic">
</codapi-snippet>

Previously, this snippet showed a blank output when run. It looked a bit confusing, and the reader might think that something was wrong.

Now, if the output is empty, the snippet will display a short reassuring ok message.