Skip to content

Commit 867a96b

Browse files
Merge pull request #15 from Promptly-Technologies-LLC/13-document-design-patterns
Added graph to illustrate PRG request-response flow
2 parents 8466273 + fd48fa3 commit 867a96b

File tree

5 files changed

+2061
-36
lines changed

5 files changed

+2061
-36
lines changed

README.md

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,45 @@
77

88
This project is still under development.
99

10-
### Install development dependencies
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+
26+
### Install development dependencies in a VSCode Dev Container
27+
28+
If you use VSCode with Docker, the following VSCode Dev Container
29+
configuration will install all dependencies and automatically open the
30+
project in a container:
31+
32+
``` json
33+
{
34+
"name": "Python 3",
35+
"image": "mcr.microsoft.com/devcontainers/python:1-3.12-bullseye",
36+
"postCreateCommand": "sudo apt update && sudo apt install -y python3-dev libpq-dev graphviz && pipx install poetry && poetry install && poetry shell",
37+
"features": {
38+
"ghcr.io/devcontainers/features/docker-outside-of-docker:1": {},
39+
"ghcr.io/rocker-org/devcontainer-features/quarto-cli:1": {}
40+
}
41+
}
42+
```
43+
44+
Simply create a .devcontainer folder in the root of the project and add
45+
a devcontainer.json file with this content, and then “Reopen in
46+
Container” from View \> Command Palette.
47+
48+
### Install development dependencies manually
1149

1250
#### Python and Docker
1351

@@ -30,8 +68,28 @@ brew install postgresql
3068

3169
For Windows:
3270

33-
- No additional system dependencies required
34-
- Install Python from the official Python website
71+
- No installation required
72+
73+
#### Quarto CLI and Graphviz
74+
75+
- [Quarto CLI](https://quarto.org/docs/get-started/)
76+
77+
For macOS:
78+
79+
``` bash
80+
brew install graphviz
81+
```
82+
83+
For Ubuntu/Debian:
84+
85+
``` bash
86+
sudo apt update && sudo apt install -y graphviz
87+
```
88+
89+
For Windows:
90+
91+
- Download and install from
92+
[Graphviz.org](https://graphviz.org/download/#windows)
3593

3694
#### Python dependencies
3795

@@ -87,6 +145,10 @@ permissions/roles are created first.
87145

88146
Navigate to http://localhost:8000/
89147

148+
### Render the README
149+
150+
`quarto render README.qmd`
151+
90152
### Contributing
91153

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

README.qmd

Lines changed: 96 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,50 +9,128 @@ format: gfm
99

1010
This project is still under development.
1111

12-
### Install development dependencies
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+
53+
### Install development dependencies in a VSCode Dev Container
54+
55+
If you use VSCode with Docker to develop in a container, the following VSCode Dev Container configuration will install all dependencies:
56+
57+
``` json
58+
{
59+
"name": "Python 3",
60+
"image": "mcr.microsoft.com/devcontainers/python:1-3.12-bullseye",
61+
"postCreateCommand": "sudo apt update && sudo apt install -y python3-dev libpq-dev graphviz && pipx install poetry && poetry install && poetry shell",
62+
"features": {
63+
"ghcr.io/devcontainers/features/docker-outside-of-docker:1": {},
64+
"ghcr.io/rocker-org/devcontainer-features/quarto-cli:1": {}
65+
}
66+
}
67+
```
1368

14-
#### Python, Docker, and Quarto CLI
69+
Simply create a `.devcontainer` folder in the root of the project and add a `devcontainer.json` file in the folder with the above content. VSCode may prompt you to install the Dev Container extension if you haven't already, and/or to open the project in a container. If not, you can manually select "Dev Containers: Reopen in Container" from View > Command Palette.
70+
71+
### Install development dependencies manually
72+
73+
#### Python and Docker
1574

1675
- [Python 3.12 or higher](https://www.python.org/downloads/)
1776
- [Docker and Docker Compose](https://docs.docker.com/get-docker/)
18-
- [Quarto CLI](https://quarto.org/docs/get-started/)
1977

2078
#### PostgreSQL headers
2179

2280
For Ubuntu/Debian:
2381

24-
```bash
82+
``` bash
2583
sudo apt update && sudo apt install -y python3-dev libpq-dev
2684
```
2785

2886
For macOS:
2987

30-
```bash
88+
``` bash
3189
brew install postgresql
3290
```
3391

3492
For Windows:
3593

36-
- No additional system dependencies required
37-
- Install Python from the official Python website
94+
- No installation required
95+
96+
#### Quarto CLI and Graphviz
97+
98+
- [Quarto CLI](https://quarto.org/docs/get-started/)
99+
100+
101+
For macOS:
102+
103+
``` bash
104+
brew install graphviz
105+
```
106+
107+
For Ubuntu/Debian:
108+
109+
``` bash
110+
sudo apt update && sudo apt install -y graphviz
111+
```
112+
113+
For Windows:
114+
115+
- Download and install from [Graphviz.org](https://graphviz.org/download/#windows)
38116

39117
#### Python dependencies
40118

41-
1. Install Poetry
119+
1. Install Poetry
42120

43-
```bash
121+
``` bash
44122
pipx install poetry
45123
```
46124

47-
2. Install project dependencies
125+
2. Install project dependencies
48126

49-
```bash
127+
``` bash
50128
poetry install
51129
```
52130

53-
3. Activate shell
131+
3. Activate shell
54132

55-
```bash
133+
``` bash
56134
poetry shell
57135
```
58136

@@ -86,7 +164,11 @@ Navigate to http://localhost:8000/
86164

87165
### Render the README
88166

89-
`quarto render README.qmd`
167+
When updating the documentation, remember to make changes in the README.qmd file, not the README.md file. Then run the following command to render the README.md file:
168+
169+
``` bash
170+
quarto render README.qmd
171+
```
90172

91173
### Contributing
92174

0 commit comments

Comments
 (0)