Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example for deploying FastHTML app using Docker #272

Merged
merged 2 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions examples/docker/fastHTML/Dockerfile
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"]
5 changes: 5 additions & 0 deletions examples/docker/fastHTML/README.md
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)
46 changes: 46 additions & 0 deletions examples/docker/fastHTML/app.py
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 added examples/docker/fastHTML/app.zip
Binary file not shown.
1 change: 1 addition & 0 deletions examples/docker/fastHTML/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
python-fasthtml
Binary file added examples/docker/fastHTML/todolist_app.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/streamlit/postgres-connection/app.zip
Binary file not shown.
Loading