Skip to content

Commit e8e8c18

Browse files
committed
✨ (Flask-Smorest) Add starter code at beginning of lecture
Use Docusaurus tabs to show starter code since it's in few files.
1 parent dfd3487 commit e8e8c18

File tree

2 files changed

+62
-30
lines changed

2 files changed

+62
-30
lines changed

docs/docs/05_flask_smorest/02_improvements_on_first_rest_api/README.md

Lines changed: 56 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -11,39 +11,16 @@ description: "Let's add a few routes to our first REST API, so it better matches
1111
- [x] Create `end` folder
1212
- [ ] Create per-file diff between `end` and `start` (use "Compare Folders")
1313

14-
## New files
14+
## Starting code from section 4
1515

16-
Let's start off by creating a `requirements.txt` file with all our dependencies:
16+
This is the "First REST API" project from Section 4:
1717

18-
```txt title="requirements.txt"
19-
flask
20-
flask-smorest
21-
python-dotenv
22-
```
23-
24-
We're adding `flask-smorest` to help us write REST APIs more easily, and generate documentation for us.
18+
import Tabs from '@theme/Tabs';
19+
import TabItem from '@theme/TabItem';
2520

26-
We're adding `python-dotenv` so it's easier for us to load environment variables and use the `.flaskenv` file.
27-
28-
Next, let's create the `.flaskenv` file:
29-
30-
```txt title=".flaskenv"
31-
FLASK_APP=app
32-
FLASK_ENV=development
33-
```
34-
35-
If we have the `python-dotenv` library installed, when we run the `flask run` command, Flask will read the variables inside `.flaskenv` and use them to configure the Flask app.
36-
37-
The configuration that we'll do is to define the Flask app file (here, `app.py`). Then we'll also set the Flask environment to `development`, which does a couple things:
38-
39-
- Sets debug mode to true, which makes the app give us better error messages
40-
- Sets the app reloading to true, so the app restarts when we make code changes
41-
42-
We don't want debug mode to be enabled in production (when we deploy our app), but while we're doing development it's definitely a time-saving tool!
43-
44-
## Code improvements
45-
46-
This is the "First REST API" project from Section 3:
21+
<div className="codeTabContainer">
22+
<Tabs>
23+
<TabItem value="app" label="app.py" default>
4724

4825
```py title="app.py"
4926
import uuid
@@ -105,6 +82,55 @@ def get_all_stores():
10582
return {"stores": list(stores.value())}
10683
```
10784

85+
</TabItem>
86+
<TabItem value="docker" label="Dockerfile">
87+
88+
```docker
89+
FROM python:3.10
90+
EXPOSE 5000
91+
WORKDIR /app
92+
RUN pip install flask
93+
COPY . .
94+
CMD ["flask", "run", "--host", "0.0.0.0"]
95+
```
96+
97+
</TabItem>
98+
</Tabs>
99+
</div>
100+
101+
102+
## New files
103+
104+
Let's start off by creating a `requirements.txt` file with all our dependencies:
105+
106+
```txt title="requirements.txt"
107+
flask
108+
flask-smorest
109+
python-dotenv
110+
```
111+
112+
We're adding `flask-smorest` to help us write REST APIs more easily, and generate documentation for us.
113+
114+
We're adding `python-dotenv` so it's easier for us to load environment variables and use the `.flaskenv` file.
115+
116+
Next, let's create the `.flaskenv` file:
117+
118+
```txt title=".flaskenv"
119+
FLASK_APP=app
120+
FLASK_ENV=development
121+
```
122+
123+
If we have the `python-dotenv` library installed, when we run the `flask run` command, Flask will read the variables inside `.flaskenv` and use them to configure the Flask app.
124+
125+
The configuration that we'll do is to define the Flask app file (here, `app.py`). Then we'll also set the Flask environment to `development`, which does a couple things:
126+
127+
- Sets debug mode to true, which makes the app give us better error messages
128+
- Sets the app reloading to true, so the app restarts when we make code changes
129+
130+
We don't want debug mode to be enabled in production (when we deploy our app), but while we're doing development it's definitely a time-saving tool!
131+
132+
## Code improvements
133+
108134
### Creating a database file
109135

110136
First of all, let's move our "database" to another file.

docs/src/css/custom.css

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,10 @@
3939

4040
[data-theme='dark'] .docusaurus-highlight-code-line {
4141
background-color: rgba(0, 0, 0, 0.3);
42+
}
43+
44+
.codeTabContainer {
45+
padding: 1rem;
46+
background-color: rgba(46, 133, 85, 0.15);
47+
border-radius: 8px;
4248
}

0 commit comments

Comments
 (0)