Admin Panel

The lila.core.admin module mounts an admin panel for your application. It uses the project database (through app.connections) and an admins table created by migrations or by lila admin.

Admin 2.0 Features

Basic Setup

Register models in app/routes/admin/index.py (the CLI creates this file for you):

from lila.core.admin import admin_routes
from lila.core.routing import Router
from app.models.user import User

router = Router()
admin_routes(models=[User], router=router, default_route="admin")

routes = router.get_routes()

admin_routes() sets router.openapi = False so admin endpoints stay out of the public schema. The admin session uses the admin_session cookie.

CLI Setup Command

To initialize the admin tables and create your first administrator:

# Setup Admin 2.0 and create/update a superuser (or: lila-admin)
lila admin --user admin --password secret

# --username is an alias; omit --password to generate one
lila admin --username admin

️ Setup and Migrations

lila admin creates the admins table itself. If you prefer plain migrations (they also add missing columns), run:

Terminal
                
lila migrate
                
            

Creating Admin Users

Admin users can be created from the command line with custom parameters:

Terminal
                
# Default usage (random password generated and printed)
lila admin

# With custom username and password
lila admin --user myadmin --password mysecurepassword

# Aliases: lila-admin / lila-create-admin / lila-create-panel-admin
lila-create-admin --username myadmin
                
            

️ Generated Routes

The admin panel mounts these routes under default_route="admin":

The templates live in resources/html/admin in your project (copied by lila admin).

️ Authentication Middleware

To protect your own routes for administrators, use @admin_required from lila.core.middleware. It checks the admin_session cookie and sets request.state.admin.

Middleware Usage
                
from lila.core.middleware import admin_required
from lila.core.admin import render_admin_menu
from lila.core.templates import render
from lila.core.request import Request

@router.get("/admin/reports")
@admin_required
async def admin_reports(request: Request):
    return render(request, "admin/reports", context={
        "menu": render_admin_menu(models=[User], request=request),
    })
                
            

System Metrics

The dashboard shows metrics captured when it renders:

Without psutil the JSON reports psutil_available: false and null values — Lila does not invent telemetry.

Logs

Request logging is enabled with App(routes=[...], logging=True), which adds LoggingMiddleware from lila.core.middleware. The logger writes date folders under app/logs/YYYY-MM-DD/; the admin log viewer reads the most recent ones, and DELETE /admin/logs clears them.

admin


admin

Creating Administrator Users (Simple Command)

The lila-create-admin alias creates or updates a single administrator without touching routes, middlewares or existing configurations:

Terminal

# Create an administrator with a specific username and password
lila-create-admin --username myadmin --password mysecretpassword

# Create an administrator with a custom username and auto-generated password
lila-create-admin --username myadmin

        

Note: the command only creates/updates the admin user in the project database (it still runs/creates the admins table). It does not generate routes or templates, so it is safe to use on an existing, customized panel.