Using Teloce-NPM with Flaxon
Flaxon 0.3+ Teloce-Py 0.3+Flaxon is a Python full-stack framework that uses Teloce as its UI layer. You write components in ui/, and Flaxon compiles them and serves a page that mounts them. This page adds npm packages to such an app.
Flaxon builds and serves the page itself, so there is no HTML file for Teloce-NPM to edit. Instead you pass the two script tags to Flaxon, and serve the generated files with a static mount.
What you need
pip install "flaxon[standard]" teloce-py teloce-npmFlaxon needs Python 3.11 or newer. flaxon[standard] includes the uvicorn server.
Project layout
my-app/
app.py
teloce-npm.json
ui/
app.html # your root component
public/
teloce-npm/ # generated by `teloce-npm install`The component
Declare the package on the first line. Wait for it in mounted():
Use dayjs from teloce-npm
<template>
<main>
<h1>Hello {{ name }}</h1>
<p id="today">{{ today }}</p>
</main>
</template>
<script>
export default {
props: ["name"],
data() { return { today: "Loading..." }; },
async mounted() {
await use.ready; // packages load after the page starts
this.today = use("dayjs")().format("dddd, MMMM D, YYYY");
}
};
</script>Configure Teloce-NPM
{
"entry_html": null,
"output_dir": "public/teloce-npm",
"base_path": "/teloce-npm"
}entry_html: nullmeans "do not inject into an HTML file".installprints the tags to load instead.output_diris outside Flaxon's build folder. Flaxon rebuilds.flaxon/buildfrom scratch on every build, so anything Teloce-NPM wrote there would be deleted.base_pathis the URL the files are served at.
Wire it into Flaxon
from pathlib import Path
from flaxon import Flaxon, Request
ROOT = Path(__file__).resolve().parent
app = Flaxon("My app", debug=True)
# Serve the files Teloce-NPM generates ...
app.mount_static("/teloce-npm", str(ROOT / "public" / "teloce-npm"))
# ... and load them from the page Flaxon generates.
app.use_teloce(
project_root=ROOT,
ui_dir="ui",
title="My app",
scripts=[
{"src": "/teloce-npm/loader.js", "defer": True},
{"src": "/teloce-npm/use.js", "defer": True},
],
)
@app.get("/")
async def home(request: Request):
return await request.compile("app.html", {"name": "Ada"})use_teloce(...) and request.compile(...) are Flaxon's documented Teloce API. scripts takes dictionaries with the attributes src, type, defer, async, integrity, crossorigin and referrerpolicy.
Install and run
teloce-npm install
flaxon run app:app --reload[4/6] Injecting scripts...
entry_html is null: no HTML file was changed. Load these tags in the page your server generates:
<script src="/teloce-npm/loader.js" defer></script>
<script src="/teloce-npm/use.js" defer></script>Open http://127.0.0.1:8000. The page shows "Hello Ada" and today's date, formatted by Day.js.
Why await use.ready matters
Flaxon's page imports your component and mounts it as soon as the module has loaded. The Teloce-NPM loader fetches its manifest and your packages at the same time. Which finishes first depends on the network, so this is a race.
I tested the same app two ways, with the package file delayed by one second to imitate a slow connection:
| Component | Result |
|---|---|
await use.ready before use("dayjs") | Shows the date |
Calls use("dayjs") directly | Stays at "Loading...", console: use(...) is not a function |
On a fast local server the second version often works anyway, which is exactly why it is easy to miss. Always wait.
Things to know
Caching
mount_static sends Cache-Control: public, max-age=3600 by default. Teloce-NPM's file names do not change when a package updates, so browsers can keep an old copy for up to an hour. Pass cache_control="no-cache" to mount_static, or serve the folder from a path you change on release.
- Scanning is already scoped. Flaxon builds with
source_rootsset to your UI folders, so Teloce-Py does not scan.teloce-npm/. You do not needstatic_dir. - Debug mode. Flaxon rebuilds components when
ui/changes. A newUseline needs ateloce-npm installto download the package. - Content-Security-Policy. Allow your own origin for scripts, and your API host in
connect-src. - Server-side rendering.
request.compile(..., ssr=True)renders on the server. Packages still run only in the browser: use them inmounted(). See Server-side rendering. - Secrets. Teloce-NPM puts no secrets in the page. Anything you pass as component context is visible in the browser, as the Flaxon docs say.