Skip to content

Commit 786b411

Browse files
Added graph to illustrate PRG request-response flow
1 parent 8466273 commit 786b411

File tree

5 files changed

+1963
-20
lines changed

5 files changed

+1963
-20
lines changed

README.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,29 @@
77

88
This project is still under development.
99

10+
### Architecture
11+
12+
This application uses a Post-Redirect-Get (PRG) pattern. The user
13+
submits a form, which sends a POST request to a FastAPI endpoint on the
14+
server. The database is updated, and the user is redirected to a GET
15+
endpoint, which fetches the updated data and re-renders the Jinja2 page
16+
template with the new data.
17+
18+
![Webapp Flow](static/webapp_flow.png)
19+
20+
The advantage of the PRG pattern is that it is very straightforward to
21+
implement and keeps most of the rendering logic on the server side. The
22+
disadvantage is that it requires an extra round trip to the database to
23+
fetch the updated data, and re-rendering the entire page template may be
24+
less efficient than a partial page update on the client side.
25+
1026
### Install development dependencies
1127

12-
#### Python and Docker
28+
#### Python, Docker, and Quarto CLI
1329

1430
- [Python 3.12 or higher](https://www.python.org/downloads/)
1531
- [Docker and Docker Compose](https://docs.docker.com/get-docker/)
32+
- [Quarto CLI](https://quarto.org/docs/get-started/)
1633

1734
#### PostgreSQL headers
1835

@@ -87,6 +104,10 @@ permissions/roles are created first.
87104

88105
Navigate to http://localhost:8000/
89106

107+
### Render the README
108+
109+
`quarto render README.qmd`
110+
90111
### Contributing
91112

92113
Fork the repository, create a new branch, make your changes, and submit

README.qmd

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,47 @@ format: gfm
99

1010
This project is still under development.
1111

12+
### Architecture
13+
14+
This application uses a Post-Redirect-Get (PRG) pattern. The user submits a form, which sends a POST request to a FastAPI endpoint on the server. The database is updated, and the user is redirected to a GET endpoint, which fetches the updated data and re-renders the Jinja2 page template with the new data.
15+
16+
``` {python}
17+
#| echo: false
18+
#| include: false
19+
from graphviz import Digraph
20+
21+
dot = Digraph()
22+
23+
dot.node('A', 'User submits form')
24+
dot.node('B', 'HTML/JS form validation')
25+
dot.node('C', 'Convert to Pydantic model')
26+
dot.node('D', 'Optional custom validation')
27+
dot.node('E', 'Update database')
28+
dot.node('F', 'Middleware error handler')
29+
dot.node('G', 'Render error template')
30+
dot.node('H', 'Redirect to GET endpoint')
31+
dot.node('I', 'Fetch updated data')
32+
dot.node('J', 'Re-render Jinja2 page template')
33+
34+
dot.edge('A', 'B')
35+
dot.edge('B', 'A')
36+
dot.edge('B', 'C', label='POST Request to FastAPI endpoint')
37+
dot.edge('C', 'D')
38+
dot.edge('C', 'F', label='RequestValidationError')
39+
dot.edge('D', 'E', label='Valid data')
40+
dot.edge('D', 'F', label='Custom Validation Error')
41+
dot.edge('E', 'H', label='Data updated')
42+
dot.edge('H', 'I')
43+
dot.edge('I', 'J')
44+
dot.edge('F', 'G')
45+
46+
dot.render('static/webapp_flow', format='png', cleanup=True)
47+
```
48+
49+
![Webapp Flow](static/webapp_flow.png)
50+
51+
The advantage of the PRG pattern is that it is very straightforward to implement and keeps most of the rendering logic on the server side. The disadvantage is that it requires an extra round trip to the database to fetch the updated data, and re-rendering the entire page template may be less efficient than a partial page update on the client side.
52+
1253
### Install development dependencies
1354

1455
#### Python, Docker, and Quarto CLI

0 commit comments

Comments
 (0)