-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Example for deploying FastHTML app using Docker (#272)
* example fasthtml app deployment * reference fasthtml example
- Loading branch information
1 parent
c5b9d13
commit 85b419b
Showing
7 changed files
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
FROM python:3.11 | ||
|
||
RUN apt-get update | ||
|
||
COPY . /app | ||
|
||
WORKDIR /app | ||
|
||
RUN pip install -r requirements.txt --no-cache-dir | ||
|
||
ENTRYPOINT ["uvicorn", "app:app", "--host=0.0.0.0", "--port=80"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Docker Deployment with FastHTML App | ||
|
||
Example Dockerfile and FastHTML app to deploy on Ploomber Cloud. The fastHTML app code was obtained from the [FastHTML examples repository](https://github.com/AnswerDotAI/fasthtml/tree/main/examples). This app is a basic todo list item tracker. To deploy your own fastHTML app, replace the `app.py` code with your code and add any Python libraries used to `requirements.txt`. | ||
|
||
![](todolist_app.png) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# This example was obtained from FastHTML's examples directory. You can check it out here: https://github.com/AnswerDotAI/fasthtml/blob/main/examples/basic_app.py | ||
|
||
# Run with: python basic_app.py | ||
from fasthtml.common import * | ||
|
||
def render(todo): | ||
show = AX(todo.title, f'/todos/{todo.id}', 'current-todo') | ||
edit = AX('edit', f'/edit/{todo.id}' , 'current-todo') | ||
dt = ' (done)' if todo.done else '' | ||
return Li(show, dt, ' | ', edit, id=f'todo-{todo.id}') | ||
|
||
app,rt,todos,Todo = fast_app('data/todos.db', render, id=int, title=str, done=bool, pk='id') | ||
|
||
@rt("/") | ||
def get(): | ||
inp = Input(id="new-title", name="title", placeholder="New Todo") | ||
add = Form(Group(inp, Button("Add")), hx_post="/", target_id='todo-list', hx_swap="beforeend") | ||
card = Card(Ul(*todos(), id='todo-list'), header=add, footer=Div(id='current-todo')), | ||
return Titled('Todo list', card) | ||
|
||
@rt("/") | ||
def post(todo:Todo): | ||
return todos.insert(todo), Input(id="new-title", name="title", placeholder="New Todo", hx_swap_oob='true') | ||
|
||
@rt("/edit/{id}") | ||
def get(id:int): | ||
res = Form(Group(Input(id="title"), Button("Save")), | ||
Hidden(id="id"), CheckboxX(id="done", label='Done'), | ||
hx_put="/", target_id=f'todo-{id}', id="edit") | ||
return fill_form(res, todos[id]) | ||
|
||
@rt("/") | ||
def put(todo: Todo): return todos.upsert(todo), clear('current-todo') | ||
|
||
@rt("/todos/{id}") | ||
def get(id:int): | ||
todo = todos[id] | ||
btn = Button('delete', hx_delete=f'/todos/{todo.id}', target_id=f'todo-{id}', hx_swap="outerHTML") | ||
return Div(Div(todo.title), btn) | ||
|
||
@rt("/todos/{id}") | ||
def delete(id:int): | ||
todos.delete(id) | ||
return clear('current-todo') | ||
|
||
serve() |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
python-fasthtml |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.