In Davia, a task is a Python function that becomes available from the frontend.

You define the logic in Python — and Davia takes care of generating the UI: input fields, buttons, and result areas.

This is the main way to let users interact with your backend: they can trigger actions, pass data, and get responses, without you writing a single line of frontend code.


Create your first task

To create a task, use the @app.task decorator. Davia will generate a UI based on the function’s inputs and output type.

from davia import Davia

app = Davia()

@app.task
def action_process_chart_data(
    start: float,
    end: float
) -> dict:
    """
    Generate chart data from start to end values.
    """
    step = (end - start) / 10
    return {
        "x": [start + i * step for i in range(11)],
        "y": [x * x for x in range(11)],
        "range": [start, end]
    }

@app.task
def display_current_time() -> str:
    """
    Returns the current time.
    """
    from datetime import datetime
    return datetime.now().strftime("%H:%M:%S")

if __name__ == "__main__":
    app.run()

When you run this file, Davia will open the editor at:

🔗 https://dev.davia.ai/dashboard


Davia and FastAPI Integration

Davia is built on top of FastAPI. When you create your app with app = Davia(), you are actually creating a FastAPI application. This means you can use all FastAPI features and add your own endpoints alongside Davia tasks.

Example: Adding a FastAPI Endpoint

from davia import Davia
from pydantic import BaseModel

app = Davia()

@app.task
def say_hello(name: str) -> str:
    return f"Hello, {name}!"

# Define a Pydantic model for request body
class Item(BaseModel):
    value: int

# Add a custom FastAPI endpoint
@app.put("/custom-endpoint")
def custom_put_endpoint(item: Item):
    return {"received": item.value}

Best Practices

Follow these essential guidelines to ensure your Davia tasks are robust, maintainable, and user-friendly:

  1. Use clear prefixes for function names
    • Start task names with action_ for operations that take inputs, and display_ for those that simply show information. This makes your codebase easier to navigate and your UI more intuitive.
  2. Always define input and output types with type annotations
    • Type annotations help Davia generate the correct UI and improve code reliability.
    @app.task
    def action_add(a: int, b: int) -> int:
        """Add two numbers and return the result."""
        return a + b
    
  3. Document your functions with concise, purpose-focused docstrings
    • A short docstring helps users understand what each task does, and can be displayed in the UI.
  4. Avoid dynamic arguments like *args or **kwargs
    • Use explicit parameters and types so Davia can generate the right input fields and validation.
  5. Keep each function focused on a single responsibility
    • Design tasks to do one thing well. This makes them easier to test, reuse, and maintain.
  6. Restart your server after adding tasks
    • When you add or modify endpoints, restart your server to ensure Davia detects all changes and updates the editor interface accordingly.

If you want to extend your app with custom endpoints or advanced features, see the FastAPI documentation.