Skip to content

Write a Python script

Goal: run a Python script against the active context in your browser: a portable script, one module with async def main(params, vf), that runs unchanged in the TUI as well.

Prerequisites

  • Signed in. Reads work for everyone; writes need an admin.
  • The context to run on is active (the run button names it); on a protected context each write asks first.
  • A current browser (WebAssembly). The first Python run of a visit loads Python (about 13 MiB from the app itself, kept by the browser afterwards), so it takes a few seconds longer.

Steps

  1. Open Infra > Scripts (route /infra/scripts) and click + New script.
  2. Python is the language a new script starts in (the list next to the name; picking TypeScript turns the untouched template into the TypeScript one). The template lists the workers, filtered by the form's Status. Or start from a Library script: Copy makes it yours (Share a script and use a template).

    A new Python script from the template, named and saved

  3. Write main. The script is one module; the app imports it and awaits main(params, vf). What it prints is the output, what it returns the result:

    async def main(params, vf):
        workers = await vf.workers.list()
        stopped = [w for w in workers if w["status"] != "RUNNING"]
        for w in stopped:
            print(w["status"], w["name"], w["id"])
        return {"stopped": len(stopped), "total": len(workers)}
    
    • params is a dict of the form's values ({} without a form); see Give the script a form, which works the same for Python.
    • vf is the same as the TUI's: await vf.<group>.<action>(input) for the catalog (Operations, on the left), await vf.<service>.<Method>(input) or await vf.call(name, input) for a reflected method, async for e in vf.topics.follow({...}) for a stream, await vf.env(name) and await vf.secret(name) for your personal entries (None when not set).
    • Name it and Save. A name is a letter, then letters, digits or _. It is listed under Mine (this browser) with · Python; Share puts it on the context for everyone there (Share a script and use a template).
    • Run on <context> (with a form: fill it in, then Run). While it runs the button is Stop.

    After the run: the printed lines, the result and the operations called

  4. Writes pass the write gate as in TypeScript: on a protected context the dialog asks; a No raises vf.OperationFailed, which the script may catch:

    try:
        await vf.workers.stop({"id": worker_id})
    except vf.OperationFailed as e:
        print("not stopped:", e)
    

What you should see

  • Output lines as the script prints; the result (the return value as JSON, or done) at the end.
  • An uncaught exception ends the run with its traceback, only your script's lines (script.py).
  • Each call under Operations called; each write in Infra > Changes.

Troubleshooting

Symptom Cause and fix
a Python script defines async def main(params, vf) The module has no main: define async def main(params, vf):.
SyntaxError with File "script.py", line N Fix that line; indentation is part of Python's syntax.
ModuleNotFoundError (numpy, virtufin_tui, …) Only the standard library is there. A portable script imports no client package and reaches the platform through vf only.
JsException / AbortError from js.fetch or pyodide.http By design: the sandbox has no network. Use vf.
OperationFailed: … declined You said No to a write (or the operation was refused: the message says why).
The first run takes long, or says HTTP 404 for a /pyodide/ file Python is loading from the app; a 404 means the app was built without its Pyodide files: tell an admin.
stopped after 10 minutes The time limit, as for TypeScript.

Next: share it on the context (Share a script and use a template); an admin can then deploy it as a worker (Deploy a script as a worker).

More: Scripts.