Skip to content

Commit

Permalink
Document running of examples, plus cleanup
Browse files Browse the repository at this point in the history
By cleanup

- Staring changing examples use fast_app()
- Check for dependencies in examples
  • Loading branch information
pydanny committed Jul 20, 2024
1 parent e50e085 commit 3454abd
Show file tree
Hide file tree
Showing 10 changed files with 137 additions and 137 deletions.
7 changes: 5 additions & 2 deletions examples/adv_app.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# Run with: python adv_app.py
# The "idiomatic" fasthtml app includes functionality from fastcore, starlette, fastlite, and fasthtml itself.
# Importing from `fasthtml.common` brings the key parts of all of these together.
# For simplicity, you can just `from fasthtml.common import *`, or you can import each symbol explicitly:
from fasthtml.common import (
# These are the HTML components we use in this app
A, AX, Button, Card, Checkbox, Container, Div, Form, Grid, Group, H1, Hidden, Input, Li, Main, Script, Style, Textarea, Title, Titled, Ul,
# These are FastHTML symbols we'll use
Beforeware, FastHTML, SortableJS, fill_form, picolink,
Beforeware, fast_app, SortableJS, fill_form, picolink, run_uv,
# These are from Starlette, Fastlite, fastcore, and the Python stdlib
FileResponse, NotFoundError, RedirectResponse, database, patch, dataclass
)
Expand Down Expand Up @@ -55,7 +56,7 @@ def before(req, sess):
# The `FastHTML` class is a subclass of `Starlette`, so you can use any parameters that `Starlette` accepts.
# In addition, you can add your Beforeware here, and any headers you want included in HTML responses.
# FastHTML includes the "HTMX" and "Surreal" libraries in headers, unless you pass `default_hdrs=False`.
app = FastHTML(before=bware,
app, rt = fast_app(before=bware,
# PicoCSS is a particularly simple CSS framework, with some basic integration built in to FastHTML.
# `picolink` is pre-defined with the header for the PicoCSS stylesheet.
# You can use any CSS framework you want, or none at all.
Expand Down Expand Up @@ -273,3 +274,5 @@ async def get(id:int):
# Therefore this will trigger the JS to parse the markdown in the details field.
# Because `class` is a reserved keyword in Python, we use `cls` instead, which FastHTML auto-converts.
return Div(Div(todo.title), Div(todo.details, cls="markdown"), btn)

if __name__ == '__main__': run_uv()
5 changes: 4 additions & 1 deletion examples/app.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Run with: python app.py
from fasthtml.common import *

id_curr = 'current-todo'
Expand All @@ -17,7 +18,7 @@ def __xt__(self):
TodoItem(id=1, title="???", done=False),
TodoItem(id=2, title="Profit", done=False)]

app = FastHTML(hdrs=(picolink, Link(rel="stylesheet", href="picovars.css")))
app, rt = fast_app(hdrs=(picolink, Link(rel="stylesheet", href="picovars.css")))

@app.get("/{fname:path}.{ext:static}")
async def image(fname:str, ext:str): return FileResponse(f'{fname}.{ext}')
Expand Down Expand Up @@ -67,3 +68,5 @@ async def get_todo(id:int):
target_id=tid(todo.id), hx_swap="outerHTML")
return Div(Div(todo.title), btn)


if __name__ == '__main__': run_uv()
1 change: 1 addition & 0 deletions examples/basic_app.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Run with: python basic_app.py
from fasthtml.fastapp import *

def render(todo):
Expand Down
1 change: 1 addition & 0 deletions examples/db_app.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Run with: uvicorn db_app:app --reload
from fasthtml.common import *

db = database('data/todos.db')
Expand Down
1 change: 1 addition & 0 deletions examples/first_app.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Run with: uvicorn first_app:app --reload
from fasthtml.fastapp import *

app,rt,todos,Todo = fast_app('data/todos.db', id=int, title=str, done=bool, pk='id')
Expand Down
10 changes: 7 additions & 3 deletions examples/image_gen_simple.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Run with: uvicorn image_gen_simple:app --reload
from sqlite_minutils import Database
from fastlite import *
from fastlite.kw import *
Expand All @@ -9,9 +10,12 @@
from fasthtml import *

import uuid
import requests
import replicate
from PIL import Image
try:
from PIL import Image
import requests
import replicate
except ImportError:
raise ImportError("Please install these dependencies: replicate, pillow, requests")

# Replicate setup (for generating images)
replicate_api_token = open("replicate_key.txt", "r").read().strip()
Expand Down
19 changes: 2 additions & 17 deletions examples/impl.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"metadata": {},
"outputs": [],
"source": [
"#|export\n",
"#|export`examples/imp.ipynb`\n",
"from dataclasses import dataclass\n",
"from typing import Any,Union,Tuple,List\n",
"\n",
Expand Down Expand Up @@ -92,22 +92,7 @@
"outputs": [
{
"data": {
"application/javascript": [
"var sel = '.cell-output, .output_area';\n",
"document.querySelectorAll(sel).forEach(e => e.classList.add('pico'));\n",
"\n",
"new MutationObserver(ms => {\n",
" ms.forEach(m => {\n",
" m.addedNodes.forEach(n => {\n",
" if (n.nodeType === 1) {\n",
" var nc = n.classList;\n",
" if (nc && (nc.contains('cell-output') || nc.contains('output_area'))) nc.add('pico');\n",
" n.querySelectorAll(sel).forEach(e => e.classList.add('pico'));\n",
" }\n",
" });\n",
" });\n",
"}).observe(document.body, { childList: true, subtree: true });"
],
"application/javascript": "var sel = '.cell-output, .output_area';\ndocument.querySelectorAll(sel).forEach(e => e.classList.add('pico'));\n\nnew MutationObserver(ms => {\n ms.forEach(m => {\n m.addedNodes.forEach(n => {\n if (n.nodeType === 1) {\n var nc = n.classList;\n if (nc && (nc.contains('cell-output') || nc.contains('output_area'))) nc.add('pico');\n n.querySelectorAll(sel).forEach(e => e.classList.add('pico'));\n }\n });\n });\n}).observe(document.body, { childList: true, subtree: true });",
"text/plain": [
"<IPython.core.display.Javascript object>"
]
Expand Down
2 changes: 1 addition & 1 deletion examples/picovars.css
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
/* Used in demonstrating how to link to a css file */
:root { --pico-font-size: 100%; }

2 changes: 2 additions & 0 deletions examples/user_app.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Run with: python user_app.py
# At signin, enter a user/pw combination and if it doesn't exist, it will be created.
from fasthtml.common import *

db = database('data/utodos.db')
Expand Down
Loading

0 comments on commit 3454abd

Please sign in to comment.