Skip to content

Jinja Template Global Variables

This document describes the global variables available in Jinja templates for the TaskRunner plugin.

Available Global Variables

task_submit_url

The task_submit_url variable contains the URL for the task submission endpoint. This URL is generated using FastAPI's url_path_for method, ensuring that it's always correct even if the application is mounted at a different path.

Usage in Templates

You can use this variable directly in your Jinja templates:

<p>The task submission URL is: {{ task_submit_url }}</p>

Usage in JavaScript

This is particularly useful for JavaScript code that needs to submit tasks via AJAX:

<script>
    async function submitTask(taskFunction, taskName, taskParams) {
        const response = await fetch('{{ task_submit_url }}', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            },
            body: JSON.stringify({
                function: taskFunction,
                name: taskName,
                kwargs: taskParams
            })
        });

        return await response.json();
    }
</script>

Implementation Details

TaskRunnerPlugin.after_setup_templates (src/plugin.py) sets the Jinja global and also exposes the same URLs to JavaScript through FastPluggy's js_global_var:

JS global Route
task_submit_url POST submit_task
url_list_available_tasks GET list_available_tasks (registered tasks)
url_task_details task details page; replace TASK_ID_REPLACE with the id
url_api_task_details GET get_task (JSON); replace TASK_ID_REPLACE with the id

It also registers the task_status_badge_class Jinja filter, which maps a status to its badge CSS class.

The request body is CreateTaskRequest (src/routers/schema.py): function (required; the registered task name), and optionally name, kwargs, topic, allow_concurrent. The response is {"task_id": "..."}.

Like every route of the plugin, submit_task requires an authenticated user when the app has an auth_manager (#27); fetch from a page of the same app sends the session cookie automatically.