Skip to main content

Add your description here

Project description

Basilico

Build HTML components in Python.

Basilico lets you create HTML components using pure Python code. Generate HTML5 markup effortlessly while building reusable components that enhance your development workflow.

Key Features

  • Write HTML components using familiar Python syntax
  • Generate standards-compliant HTML5 output
  • Create and maintain reusable components
  • Keep your templates type-safe with Python's typing
  • No external dependencies

Usage

pip install basilico
from basilico.attributes import Class, Href
from basilico.components import If
from basilico.elements import A, Li, Nav, Ol, Text
from basilico.node import Node


def navbar(logged_in: bool) -> Node:
    return Nav(
        Class("navbar"),
        Ol(
            navbar_item("Home", "/"),
            navbar_item("Contact", "/contact"),
            navbar_item("About", "/about"),
            If(
                logged_in is False,
                navbar_item("Login", "/login"),
            ),
        ),
    )


def navbar_item(name: str, path: str) -> Node:
    return Li(A(Href(path), Text(name)))

We can use generators to create components inline:

from basilico.elements import Li, Text, Ul

items = ["Coffee", "Tea", "Milk"]
Ul(
    *(Li(Text(i)) for i in items),
)

# <ul>
#     <li>Coffee</li>
#     <li>Tea</li>
#     <li>Milk</li>
# </ul>

HTMX support:

from basilico import htmx as hx
from basilico.elements import Button

Button(hx.Get("/example"))

# <button hx-get="/example"></button>

Alpine JS support:

from basilico import alpine as x
from basilico.elements import Button, Div, Text

Div(
    x.Data("{ open: false }"),
    Button(x.On("click", "open = !open"), Text("Toggle")),
    Div(x.Show("open"), Text("Hello, Basilico!")),
)

# <div x-data="{ open: false }">
#     <button x-on:click="open = !open">Toggle</button>
#     <div x-show="open">Hello, Basilico!</div>
# </div>

FastAPI example:

from fastapi import FastAPI
from fastapi.responses import HTMLResponse

from basilico.attributes import Class, Src
from basilico.components import HTML5, HTML5Props
from basilico.elements import H1, Div, Script, Text
from basilico.node import Node

app = FastAPI()


def page(title: str, description: str, body: Node) -> Node:
    props = HTML5Props(
        title=title,
        description=description,
        language="en",
        head=[Script(Src("https://cdn.tailwindcss.com"))],
        body=[body],
    )
    return HTML5(props)


def body() -> Node:
    return Div(
        Class("min-h-screen flex items-center justify-center"),
        H1(
            Class("text-2xl font-bold text-teal-600 text-7xl"),
            Text("Hello, Basilico!"),
        ),
    )


@app.get("/")
def home():
    content = page(
        title="Home",
        description="This is a simple example of Basilico with FastAPI.",
        body=body(),
    )

    return HTMLResponse(content.string())

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

basilico-0.1.0.tar.gz (19.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

basilico-0.1.0-py3-none-any.whl (8.7 kB view details)

Uploaded Python 3

File details

Details for the file basilico-0.1.0.tar.gz.

File metadata

  • Download URL: basilico-0.1.0.tar.gz
  • Upload date:
  • Size: 19.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.9

File hashes

Hashes for basilico-0.1.0.tar.gz
Algorithm Hash digest
SHA256 ce411ce9339a1f7c5384ef26c8d115507acce68022e31ea63e449284a4d42102
MD5 569d5cf22542e38a9fb9811203d7dc71
BLAKE2b-256 1c91cd243e5c94fe50a562a991067282bb6d994a4bd6758d66b8f8c1687d7804

See more details on using hashes here.

File details

Details for the file basilico-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: basilico-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 8.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.9

File hashes

Hashes for basilico-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 17d7622b276718c390003a844fcfb4a646835d066cd50ed815c79f133ceca3f5
MD5 cda1afb25c5791b43fae8f6176671e13
BLAKE2b-256 5b3da2670319d9bee57d9c6a967d8d63dfc4cb0f87040656c19b08f8467c4c6e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page