Skip to content

Commit

Permalink
chore: move files to app.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Darkflame72 committed Mar 28, 2024
1 parent b2db179 commit 2c26384
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 27 deletions.
8 changes: 4 additions & 4 deletions src/content/docs/guides/python/flask/blueprints.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@ sidebar:
order: 7
---

Following [PEP 20](https://peps.python.org/pep-0020/) which states that "Simple is better than complex", we will be simplifying our code by using blueprints. Blueprints are a way to organize a group of related views and other code. They are registered with the application and can be used to create a modular application.
Following [PEP 20](https://peps.python.org/pep-0020/) which states that "Simple is better than complex", we will be simplifying our code by using [blueprints](https://flask.palletsprojects.com/en/3.0.x/blueprints/). Blueprints are a way to organize a group of related views and other code. They are registered with the application and can be used to create a modular application.

import { FileTree } from '@astrojs/starlight/components';

<FileTree>

- main.py
- app.py

</FileTree>

This gets turned into a tree structure like this:

<FileTree>

- main.py
- app.py
- blog.py
- auth.py
- ...
Expand All @@ -44,7 +44,7 @@ def post(id: int):
return f'Post {id}'
```

We then need to register the blueprint with the application in `main.py`.
We then need to register the blueprint with the application in `app.py`.

```python
from flask import Flask
Expand Down
8 changes: 6 additions & 2 deletions src/content/docs/guides/python/flask/forms.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ So far all of this work replicates what can already be done with a simple HTML f
In this section, we will create a simple website that allows users to update data. We will use a simple list of dictionaries to store the data. This is not a scalable solution, but it is a good starting point.

```python
// main.py
// app.py
from flask import Flask, render_template, request

app = Flask(__name__)
Expand Down Expand Up @@ -92,4 +92,8 @@ import { Steps } from '@astrojs/starlight/components';
Make sure to add the `methods=["POST"]` argument to the route decorator to allow POST requests.
:::

</Steps>
</Steps>

## Using WTF-Forms

https://flask.palletsprojects.com/en/3.0.x/patterns/wtforms/
2 changes: 1 addition & 1 deletion src/content/docs/guides/python/flask/routes.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ Now when you visit the home page, about page, or contact page, you will see the

- Home page [127.0.0.1:5000/](http://127.0.0.1:5000/) - `<h1>Welcome to the home page!</h1>`
- About page [127.0.0.1:5000/about](http://127.0.0.1:5000/about) - `<h1>About us</h1><p>This is the about page.</p>`
- Contact page [127.0.0.1:5000/contect](http://127.0.0.1:5000/contact) - `<h1>Contact us</h1><p>Contact us at: <a href='mailto:[email protected]'>[email protected]</a></p>`
- Contact page [127.0.0.1:5000/contact](http://127.0.0.1:5000/contact) - `<h1>Contact us</h1><p>Contact us at: <a href='mailto:[email protected]'>[email protected]</a></p>`


## Variable routes
Expand Down
76 changes: 56 additions & 20 deletions src/content/docs/guides/python/flask/templates.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@ sidebar:
order: 3
---

Writing HTML in Python code is not a good practice. It is hard to maintain, understand and becomes really difficult with larger pages. Flask provides a way to write HTML in separate files and then use them in the Python code. These files are called templates which are built using [Jinja2](https://jinja.palletsprojects.com/en/3.0.x/).
Writing HTML in Python code is not a good practice. It is hard to maintain, understand and becomes really difficult with larger pages. Flask provides a way to write HTML in separate files and then use them in the Python code. These files are called [templates](https://flask.palletsprojects.com/en/3.0.x/templating/) which are built using [Jinja2](https://jinja.palletsprojects.com/en/3.0.x/).

## Creating a Template
## What are Templates

Create a new folder called `templates` in the project directory. Inside this folder, create a new file called `index.html`. Add the following code to the file:
Templates are special HTML files that contain placeholders for dynamic content. These placeholders are replaced with actual values when the template is rendered. Templates are used to separate the presentation logic from the business logic.

Flask stores all of its templates in a folder called `templates`. This folder should be created in the same directory as the `app.py` file.

### Creating a Template

```html
// templates/index.html
Expand All @@ -26,16 +30,22 @@ Create a new folder called `templates` in the project directory. Inside this fol
</html>
```

:::note
What is this??? `{{ title }}`, `{{ heading }}`, `{{ content }}` are called placeholders. These are replaced with actual values when the template is rendered.
:::
import { Steps } from '@astrojs/starlight/components';

<Steps>

1. The overall file is HTML so requires all the base HTML tags

2. Each part that would contain content is instead replaced by a placeholder enclosed in double curly braces `{{ }}`. These placeholders are replaced with actual values when the template is rendered.

## Rendering a Template
</Steps>

### Rendering a Template

To render a template, we need to use the `render_template` function from the `flask` module. This function takes the name of the template file and the values to be replaced in the placeholders.

```diff lang="python"
// main.py
// app.py
- from flask import Flask
+ from flask import Flask, render_template

Expand Down Expand Up @@ -85,6 +95,21 @@ Base templates can be used so you don't have to repeat the same code in every te
</html>
```

<Steps>

1. The `base.html` file contains the basic structure of an HTML file.

2. The `title` and `content` sections are enclosed in `{% block %}` tags. These are placeholders that can be overridden in the child templates.

Instead of putting content from a variable inside of these tags they will instead be filled with a block of content from the child template.
Instead of using `{{ variable_name }}` we use `{% block block_name %}{% endblock %}` which indicates it is a block instead of a variable.

3. As these are blocks, they can be overridden in the child templates by using the same block name.

They are overridden by using the `{% block %}` tag with the same name as the block in the parent template.

</Steps>

We will then need to update our `index.html` file to extend the `base.html` file.

```diff lang="html"
Expand All @@ -111,6 +136,7 @@ We will then need to update our `index.html` file to extend the `base.html` file
This would give a `index.html` that now looks like this:

```html
// templates/index.html
{% extends 'base.html' %}

{% block title %}Home{% endblock %}
Expand All @@ -121,6 +147,16 @@ This would give a `index.html` that now looks like this:
{% endblock %}
```

<Steps>

1. Using `{% extends 'base.html' %}` we indicate that the `index.html` file extends the `base.html` file. This means that the content of the `index.html` file will be inserted into the `base.html` file.

2. We then use `{% block title %}Home{% endblock %}` to override the `title` block in the `base.html` file.

3. We then use `{% block content %}...{% endblock %}` to override the `content` block in the `base.html` file.

</Steps>

When we run the server and visit the home page, we will see the content from the template file.

```html
Expand All @@ -144,7 +180,7 @@ Includes can be used to include a file in another file. Create a new file called
```html
// templates/header.html
<header>
<h1>Website</h1>
<h1>Home</h1>
</header>
```

Expand All @@ -170,7 +206,7 @@ When we run the server and visit the home page, we will see the content from the

<body>
<header>
<h1>Website</h1>
<h1>Home</h1>
</header>
<h1>Welcome to the website</h1>
<p>This is a simple website built using Flask</p>
Expand All @@ -182,14 +218,14 @@ When we run the server and visit the home page, we will see the content from the
You can also use `extends` and `include` together to create a complex template structure.
:::

#### Passing Variables to Includes
#### Passing Variables to Templates

If you want to pass variables to the included file, you can do so by passing the variables as arguments to the `include` function. We can modify our `header.html` file to accept a `title` variable.

```diff lang="html"
// templates/header.html
<header>
- <h1>Website</h1>
- <h1>Home</h1>
+ <h1>{{ title }}</h1>
</header>
```
Expand All @@ -200,7 +236,7 @@ We will then need to update our `base.html` file to include the `header.html` fi
// templates/base.html
<body>
- {% include 'header.html' %}
+ {% include 'header.html' with title='Website' %}
+ {% include 'header.html' with title={{ title }} %}
{% block content %}{% endblock %}
</body>
```
Expand All @@ -216,7 +252,7 @@ When we run the server and visit the home page, we will see the content from the

<body>
<header>
<h1>Website</h1>
<h1>Home</h1>
</header>
<h1>Welcome to the website</h1>
<p>This is a simple website built using Flask</p>
Expand Down Expand Up @@ -244,10 +280,10 @@ If we have a list of items that we want to display, we can use a loop to iterate
{% endblock %}
```

We will then need to update our `main.py` file to render the `list.html` file.
We will then need to update our `app.py` file to render the `list.html` file.

```python
// main.py
// app.py
@app.route('/list')
def list():
items = ['Item 1', 'Item 2', 'Item 3']
Expand All @@ -274,8 +310,8 @@ When we run the server and visit the list page, we will see the content from the
</html>
```

:::note
This can also be combined with `includes` to have an entire html section as a separate file.
:::tip
This can also be combined with `includes` to have use an html section for each instance of the data.
:::

### Conditionals
Expand All @@ -298,10 +334,10 @@ If we want to display content based on a condition, we can use conditionals. Cre
{% endblock %}
```

We will then need to update our `main.py` file to render the `conditional.html` file.
We will then need to update our `app.py` file to render the `conditional.html` file.

```python
// main.py
// app.py
@app.route('/conditional')
def conditional():
condition = True
Expand Down
2 changes: 2 additions & 0 deletions src/content/docs/guides/python/flask/testing.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ sidebar:
order: 9
---

https://flask.palletsprojects.com/en/3.0.x/testing/

Whenever things are built it is important to test them. Testing can be done manually, but this is time consuming and prone to human error. It is better to automate your testing. This will allow you to test your code more frequently and ensure that it is working as expected. For this application unit tests will be used to make sure that all routes function as intended.

:::note
Expand Down

0 comments on commit 2c26384

Please sign in to comment.