Skip to main content

A utility package for Caspian projects

Project description

Caspian Utils (casp)

HTML-first utilities for Caspian applications.

caspian-utils is the shared Python runtime package behind Caspian templates, components, layouts, RPC handlers, and supporting utilities. This repository is not a full application starter, so this README documents the package surface that exists here.

  • PyPI package: caspian-utils
  • Python package: casp
  • Python requirement: >=3.14

Installation

pip install caspian-utils

Core Model

Caspian is HTML-first.

  • write UI in .html
  • define reusable components in Python
  • import those components with @import comments inside HTML
  • render them with <x-component-name /> tags
  • render pages and layouts from Python with helpers in casp.layout

This matches the current compiler behavior in this package.

HTML-First Components

Define a component in Python

from casp.component_decorator import component


@component
def AlertBox(title: str, children: str = "", class_name: str = "") -> str:
    return f"""
    <section class="alert {class_name}">
        <h2>{title}</h2>
        <div>{children}</div>
    </section>
    """

Import and use it in HTML

<!-- @import { AlertBox } from "../components/ui" -->

<main class="space-y-4">
  <x-alert-box title="Saved">
    <p>Your settings were updated.</p>
  </x-alert-box>
</main>

Alias imports when needed

<!-- @import { AlertBox as Notice } from "../components/ui" -->

<div>
  <x-notice title="Heads up" />
</div>

Import and tag rules:

  • AlertBox becomes <x-alert-box>
  • aliases also convert to kebab-case, so Notice becomes <x-notice>
  • grouped imports use <!-- @import { A, B as C } from "..." -->
  • single imports use <!-- @import ComponentName from "..." -->
  • paths are resolved relative to the current template or component directory

Example: <!-- @import { AlertBox } from "../components/ui" --> resolves AlertBox from ../components/ui/AlertBox.py.

Rendering Pages and Layouts

Use casp.layout to render HTML templates relative to a Python file.

from casp.layout import render_page


def get_dashboard() -> str:
    return render_page(__file__, {
        "pageTitle": "Dashboard",
        "stats": ["Projects", "Tasks", "Alerts"],
    })
<!-- app/dashboard/index.html -->
<!-- @import { AlertBox } from "../components/ui" -->

<main class="space-y-6">
  <h1>[[ pageTitle ]]</h1>

  <x-alert-box title="Welcome back" class="rounded border p-4">
    <p>Rendered through an imported Python component.</p>
  </x-alert-box>

  <ul>
    <template casp-for="label in stats">
      <li>[[ label ]]</li>
    </template>
  </ul>
</main>

casp.layout also exposes render_layout(), render(), load_template(), and layout discovery helpers for nested layout flows.

Template Syntax Supported Here

This package supports the Caspian syntax that is transpiled into Jinja-compatible templates:

  • [[ value ]] interpolation
  • <template casp-for="..."></template> loops
  • <template casp-if="..."></template> conditionals
  • imported component rendering through <x-...> tags
  • dynamic template compilation and nested layout rendering

Template Constraints

Some compiler rules are important when writing templates:

  • every page, layout, and component must render exactly one top-level native HTML element
  • the root element cannot be an <x-...> component tag
  • unknown <x-...> tags raise an error unless the component has been imported
  • async components are supported by the component pipeline

These constraints exist because Caspian injects pp-component metadata into the rendered root element.

RPC Helpers

The package also includes the server-side RPC decorator and related request/serialization utilities.

from casp.rpc import rpc


@rpc(require_auth=True, limits="30/minute")
async def save_profile(name: str):
    return {"ok": True, "name": name}

casp.rpc includes:

  • RPC registration
  • auth-aware decorators
  • rate limiting
  • serialization for common Python objects
  • FastAPI-oriented request and response helpers

Main Modules

Module Purpose
casp.layout Load, compile, and render pages and nested layouts
casp.component_decorator @component, component loading, and HTML rendering helpers
casp.components_compiler Parse @import directives and transform <x-...> component tags
casp.syntax_compiler Transpile Caspian HTML syntax into Jinja-compatible templates
casp.rpc RPC decorator, registration, serialization, and request handling helpers
casp.streaming Server-Sent Events helpers including SSE
casp.validate Validation and sanitization helpers for strings, IDs, files, dates, and numbers
casp.caspian_config Config loading and file index helpers

Included Dependencies

This release currently installs utilities commonly used by Caspian apps, including:

  • fastapi
  • uvicorn
  • jinja2
  • python-dotenv
  • slowapi
  • starsessions
  • python-multipart
  • httpx
  • tailwind-merge
  • werkzeug
  • cuid2
  • nanoid
  • python-ulid

See setup.py for the pinned versions in this release.

Repository

TheSteelNinjaCode/caspian_utils

License

MIT

Author

Jefferson Abraham

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

caspian_utils-0.2.11.tar.gz (48.4 kB view details)

Uploaded Source

Built Distribution

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

caspian_utils-0.2.11-py3-none-any.whl (51.5 kB view details)

Uploaded Python 3

File details

Details for the file caspian_utils-0.2.11.tar.gz.

File metadata

  • Download URL: caspian_utils-0.2.11.tar.gz
  • Upload date:
  • Size: 48.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for caspian_utils-0.2.11.tar.gz
Algorithm Hash digest
SHA256 547d84fde11dd4f998e36ce695806f4e59e6da8415d493f411b27936f5c03a4c
MD5 f3bc112336b4a604d08b2785f47e9895
BLAKE2b-256 0b51c92ff1ece68d24c5388ba071b710af4b4da9f0e55691e7174a3ea66e305b

See more details on using hashes here.

File details

Details for the file caspian_utils-0.2.11-py3-none-any.whl.

File metadata

  • Download URL: caspian_utils-0.2.11-py3-none-any.whl
  • Upload date:
  • Size: 51.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for caspian_utils-0.2.11-py3-none-any.whl
Algorithm Hash digest
SHA256 9cbd8b1a8922ebc9c298c17277b99818e1113b8f294fd5dea11e1706fcb3a1dd
MD5 4c29ec6ad23fea6f3fa89aef456b0639
BLAKE2b-256 26bd31b3b2c9e8172dda8331e3b2bb5e61fb1a31ab89e93f4b394df678f2acff

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