From 6a9fc0a72affcf3986b35fedcc22bb022e23e625 Mon Sep 17 00:00:00 2001 From: Andros Fenollosa Date: Tue, 18 Jun 2024 14:50:41 +0200 Subject: [PATCH] Fist commit --- Dockerfile | 7 ++++++ about_us.py | 19 +++++++++++++++ compose.yaml | 7 ++++++ home.py | 10 ++++++++ main.py | 45 +++++++++++++++++++++++++++++++++++ requirements.txt | 12 ++++++++++ static/.gitkeep | 0 templates/layouts/base.html | 42 ++++++++++++++++++++++++++++++++ templates/pages/about_us.html | 11 +++++++++ templates/pages/home.html | 6 +++++ 10 files changed, 159 insertions(+) create mode 100644 Dockerfile create mode 100644 about_us.py create mode 100644 compose.yaml create mode 100644 home.py create mode 100644 main.py create mode 100644 requirements.txt create mode 100644 static/.gitkeep create mode 100644 templates/layouts/base.html create mode 100644 templates/pages/about_us.html create mode 100644 templates/pages/home.html diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..e11678e --- /dev/null +++ b/Dockerfile @@ -0,0 +1,7 @@ +FROM base + +COPY ./requirements.txt . +RUN pip3 install -r requirements.txt + +# launcher +CMD fastapi dev main.py --host 0.0.0.0 diff --git a/about_us.py b/about_us.py new file mode 100644 index 0000000..9c6a2c8 --- /dev/null +++ b/about_us.py @@ -0,0 +1,19 @@ +from datetime import datetime + +async def send_page(websocket, templates, data_frontend): + await websocket.send_json( + { + 'selector': '#main', + 'html': templates.get_template( + f'pages/about_us.html' + ).render(), + } + ) + +async def current_time(websocket, templates, data_frontend): + await websocket.send_json( + { + 'selector': '#time', + 'html': f"{datetime.now()}", + } + ) diff --git a/compose.yaml b/compose.yaml new file mode 100644 index 0000000..d457235 --- /dev/null +++ b/compose.yaml @@ -0,0 +1,7 @@ +services: + fastapi: + build: . + ports: + - "8000:8000" + volumes: + - .:/usr/src/app diff --git a/home.py b/home.py new file mode 100644 index 0000000..a8550f2 --- /dev/null +++ b/home.py @@ -0,0 +1,10 @@ + +async def send_page(websocket, templates, data_frontend): + await websocket.send_json( + { + 'selector': '#main', + 'html': templates.get_template( + f'pages/home.html' + ).render(), + } + ) diff --git a/main.py b/main.py new file mode 100644 index 0000000..44c74ac --- /dev/null +++ b/main.py @@ -0,0 +1,45 @@ +import os +import pathlib + +from fastapi import FastAPI, Request, WebSocket +from fastapi.staticfiles import StaticFiles +from fastapi.templating import Jinja2Templates + +import home +import about_us + +app = FastAPI() +app.mount('/static', StaticFiles(directory='static'), name='static') + +BASE_DIR = pathlib.Path(__file__).parent +templates = Jinja2Templates( + directory=[ + BASE_DIR / 'templates', + ] +) + + +@app.get('/') +async def welcome_page(request: Request): + context = { + 'request': request, + } + return templates.TemplateResponse( + 'layouts/base.html', context, + ) + + + +@app.websocket('/ws/liveview/') +async def websocket_endpoint(websocket: WebSocket): + await websocket.accept() + while True: + data_frontend = await websocket.receive_json() + if data_frontend and 'action' in data_frontend: + action_data = data_frontend['action'].split('->') + if len(action_data) == 2: + action = action_data[0].lower() + function = action_data[1].lower() + await eval( + f'{action}.{function}(websocket, templates, data_frontend)' + ) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..6593a9d --- /dev/null +++ b/requirements.txt @@ -0,0 +1,12 @@ +fastapi +ruff +fastapi-cli +email_validator +pydantic-settings +pydantic-extra-types +httpx +jinja2 +python-multipart +uvicorn +orjson +ujson diff --git a/static/.gitkeep b/static/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/templates/layouts/base.html b/templates/layouts/base.html new file mode 100644 index 0000000..daa505e --- /dev/null +++ b/templates/layouts/base.html @@ -0,0 +1,42 @@ + + + + + Example + + + + + + +
+

FastAPI with LiveView

+ +
+
+ + + diff --git a/templates/pages/about_us.html b/templates/pages/about_us.html new file mode 100644 index 0000000..cd57847 --- /dev/null +++ b/templates/pages/about_us.html @@ -0,0 +1,11 @@ +
+

About us

+

+ +

+

+
diff --git a/templates/pages/home.html b/templates/pages/home.html new file mode 100644 index 0000000..f0a27e3 --- /dev/null +++ b/templates/pages/home.html @@ -0,0 +1,6 @@ +
+

Home

+

+ In dictum non, consectetur a erat nam at lectus urna duis convallis convallis? Duis tristique sollicitudin nibh sit amet commodo nulla facilisi nullam vehicula ipsum a arcu cursus vitae congue mauris rhoncus aenean vel elit? Aliquet sagittis id consectetur purus. Pellentesque adipiscing commodo elit, at imperdiet dui accumsan sit. Praesent elementum facilisis leo, vel fringilla est ullamcorper eget nulla facilisi etiam dignissim diam quis enim lobortis scelerisque fermentum dui faucibus in ornare quam viverra orci sagittis eu volutpat! Dui, id ornare arcu odio ut sem nulla pharetra diam sit amet nisl suscipit adipiscing bibendum est ultricies integer quis! Et leo duis ut diam quam nulla porttitor massa id neque! Morbi non arcu risus, quis varius quam quisque id diam vel quam elementum pulvinar etiam! Id faucibus nisl tincidunt eget nullam non nisi est, sit amet facilisis magna etiam tempor, orci eu lobortis elementum, nibh tellus molestie nunc, non blandit massa enim nec dui nunc! Cras ornare arcu dui vivamus arcu felis, bibendum ut tristique et, egestas quis ipsum suspendisse ultrices gravida dictum fusce ut placerat orci nulla pellentesque dignissim! Commodo viverra maecenas accumsan, lacus vel facilisis volutpat, est velit egestas dui, id ornare arcu odio ut sem nulla pharetra diam. +

+