For larger programs, you can pass multiple files along with the main one.
Suppose you have a Python program with an npc
module that you want to call from the main module. In this case, do the following:
- Prepare a file
npc.py
:
def greet(name):
print(f"Hello, {name}")
- Create a snippet with the actual code:
<pre><code>
import npc
npc.greet("Alice")
</code></pre>
<codapi-snippet sandbox="python" files="npc.py"></codapi-snippet>
- Host the
npc.py
next to the web page containing thecodapi-snippet
.
Now codapi-snippet
will send npc.py
to the server along with the main file.
To pass multiple files, separate them with space:
<codapi-snippet sandbox="python" files="file1.py file2.py"></codapi-snippet>
You can define files using in-page script
s:
<script id="npc.py" type="text/plain">
def greet(name):
print(f"Hello, {name}")
</script>
<pre><code>
import npc
npc.greet("Alice")
</code></pre>
<codapi-snippet sandbox="python" files="#npc.py"></codapi-snippet>
The use of id
and #
is mandatory; you can't select files based on CSS classes or other attributes.
You can also use another codapi-snippet
instead of a script:
<pre><code>
def greet(name):
print(f"Hello, {name}")
</code></pre>
<codapi-snippet id="npc.py" sandbox="python"></codapi-snippet>
<pre><code>
import npc
npc.greet("Alice")
</code></pre>
<codapi-snippet sandbox="python" files="#npc.py"></codapi-snippet>
You can set a different name for a file using :
:
<pre><code>
import greeter
greeter.greet("Alice")
</code></pre>
<codapi-snippet sandbox="python" files="npc.py:greeter.py"></codapi-snippet>
In this example, the local npc.py
is renamed to greeter.py
in the sandbox.
This can be useful if the sandbox expects a fixed filename (e.g., config.json
), but your code snippets use different local files with the same semantics (e.g., config-1.json
, config-2.json
, ...)