Skip to main content

Python & Django (prismds)

Server-rendered Prism components for Django, Flask, FastAPI or any Python app that renders HTML. The same markup and CSS classes as Prism's React library, from plain Python:

  • No React, no Node, no build step. You add a Python package, four stylesheets and one small vanilla-JS file (prism.js, loaded with defer).
  • One version with the rest of Prism. prismds 2.14.0 ships exactly the CSS that the React registry ships at 2.14.0.
  • Safe by default. Text and attribute values are escaped, javascript: URLs are blocked, and event-handler attributes need explicitly trusted values.
  • No runtime dependencies. Django and Jinja2 integrations load only when you use them.

Install​

Pin the wheel by its URL. This is the recommended way, because it is immune to dependency confusion:

# requirements.txt
prismds @ https://prism.artoftechconsulting.com/python/prismds-2.14.0-py3-none-any.whl

Or install from the simple index (useful for pip index versions prismds):

pip install --index-url https://prism.artoftechconsulting.com/python/simple/ prismds==2.14.0

Don't use --extra-index-url: pip would also look on public PyPI.

Django​

# settings.py
INSTALLED_APPS = [..., "django.contrib.staticfiles", "prismds"]
PRISM = {"signature": "commerce", "theme": "light"} # theme: light | dark | system | industry[-dark|-system] | enterprise[-dark|-system]
{% load prism %}
<!DOCTYPE html>
<html lang="en" {% prism_html_attrs %}>
<head>
{% prism_head %} {# fonts, tokens, component CSS, prism.js #}
</head>
<body>
{% prism "page-header" title="Orders" eyebrow="Fulfilment" %}

{% prism "button" "Save" variant="accent" type="submit" %}
{% prism "badge" order.status_label tone="positive" %}

{% prismblock "card" padded=True %}
<h3>Summary</h3> …any template content…
{% endprismblock %}

{% prism_form form %} {# every field as a Prism field, errors wired #}
{% prism_field form.email width="lg" %}
{% prism_messages %} {# django.contrib.messages as Prism toasts #}
</body>
</html>
  • {% prism "name" children… key=value… %} renders a component. Its first positional argument is the children: a label, a value, or a mark_safe fragment.
  • {% prismblock "name" key=value %}…{% endprismblock %} passes the enclosed template content as the children.
  • Props are the React props in snake_case: isDisabled → is_disabled, iconEnd → icon_end, className → class_name. Any other keyword (id, title, aria_label, data_testid) becomes an attribute on the component's root element.
  • python manage.py check validates the PRISM setting.
  • collectstatic, ManifestStaticFilesStorage and CDN storage all work: fonts are referenced relative to their stylesheet.

Flask / FastAPI / Jinja2​

import prismds
from prismds.jinja import install

install(env, static_url="/static/", signature="commerce") # env = app.jinja_env (Flask) or templates.env (FastAPI)

# serve the assets
# FastAPI: app.mount("/static", StaticFiles(directory=prismds.static_dir()), name="static")
# Flask: Flask(__name__, static_folder=str(prismds.static_dir()), static_url_path="/static")
<html lang="en" {{ prism.html_attrs() }}>
<head>{{ prism.head() }}</head>
{{ prism.button("Save") }}
{% call prism.card(padded=True) %} …content… {% endcall %}

Plain Python​

from prismds import ui, head, html_attrs
html = f"<html {html_attrs('commerce')}><head>{head('/static/')}</head><body>{ui.badge('Paid', tone='positive')}</body></html>"

Components return Markup, a str subclass with __html__, so Django, Jinja2, MarkupSafe and most template engines insert it without escaping it again.

Interactivity without React​

prism.js (vanilla, about 30 KB, 8 KB gzipped) adds behaviour to server-rendered markup: dialogs (native <dialog>), confirmation before a form submit or link (data-prism-confirm), tabs, accordion, menus, tooltips, toasts (Prism.toast()), the AppHeader drawer, sidebar collapse and copy buttons. Pages still render and submit without it. Components that only make sense as React widgets, such as DataGrid, the rich-text editor and the React charts, aren't part of this package. Server-rendered equivalents (Table, native date and time inputs, native selects) are.

Same HTML as React​

Every component is checked against its React twin on each change to Prism. CI renders both from the same props and fails on any difference in the HTML. Server-only deviations (sort headers as links, native <select>, native date inputs) are documented on each component.

Components​

Run python -m prismds for the list of components and their arguments.