')
def show_it(something):
return render_template('bar.html', title=something, paragraph_text=something)
```
--
- if a web browser makes a request for `show/me`, then the variable `something` will hold the value `"me"`; if a web browser makes a requests for `/show/25251`, then the variable `something` will hold the value `25251`; and so on.
--
- in this example, for simplicity, we pass along the value of `something` as the `title` and `paragraph_text` value for the [foo.html template](#foo-template) defined earlier.
---
template: flask
## Linking from a template to a route
In `flask`'s templating engine, it is possible to automatically generate the correct links from a template to a route by using the `url_for()` method.
--
For example, this code, if placed within a template file, would link to the [/foo route](#foo-route):
```html
click me
```
--
- note that the `url_for()` function expects to receive the name of the function associated with the target route, not the route URL itself.
---
template: flask
## Linking from a template to a route (continued)
If the target route expects arguments to be supplied to it, such as our parameterized [/show/something](#show-something-route) route did, then these can be supplied as additional arguments to `url_for()`:
--
For example, this code, if placed within a template file, would link to the `show/something` route, with `"me"` passed into the `something` variable:
```html
click me
```
---
template: flask
## Base templates
Most pages of a web site share a lot of HTML code in common - especially the top and bottom sections of th HTML.
--
- `flask` allows you to create a **base template** containing the common code that will be incorporated automatically into each page.
--
- this allows your template HTML files to focus on what is different from one page to the next.
---
template: flask
## Base templates (continued)
For example, here is a simple base template in a file named `templates`/`base.html` containing boilerplate HTML code with a block named `container` in the middle for each page's unique content to be placed.
```html
My Very Ordinary Site
{% block container %} {% endblock %}
```
---
template: flask
## Base templates (continued again)
Other templates can now indicate that their contents should be wrapped inside the base template code.
--
For example, here is a template named `templates`/`baz.html`, which will automatically include the code from the base template.
```html
{% extends 'base.html' %} {% block container %}
Put the unique content of this template between the begin/end of the container
block
{% endblock %}
```
---
name: combined
# Pymongo + Flask
--
## Concept
A modern web application inevitably involves:
--
- front-end code (always HTML and CSS, usually with some Javascript)
--
- back-end code (in our case Python with `flask`)
--
- database (in our case MongoDB connected from Python with `pymongo`)
---
template: combined
## Routes with database integration
It is, of course, possible to develop `flask` routes that interact with a MongoDB database using `pymongo`.
--
For example, here is a route that pulls documents from a database and passes them to a template:
```python
@app.route('/read')
def read():
docs = db.collection_name.find({})
return render_template('read.html', docs=docs) # render the read template, passing it the list of documents
```
--
- this assumes that `app` has alrady been [defined as a Flask app](#flask-instantiation) and `db` has been defined as a [connection to a database](#pymongo-connection).
--
- the template can now [iterate through the list](#flask-template-iteration) of documents and output some details about them as HTML.
--
- ... and much more.
---
name: useful-modules
# Useful modules
--
## Concept
Since flask is a popular Python web framework, there are many useful modules that can be used in tandem with it, e.g.:
- [python-dotenv](https://pypi.org/project/python-dotenv/) - to load environment variables from a `.env` file into `os.environ` in development
- [flask-login](https://pypi.org/project/Flask-Login/) - to manage user sessions in a Flask app
- [flask-mail](https://pypi.org/project/Flask-Mail/) - to send email from a Flask app
- [flask-socketio](https://pypi.org/project/Flask-SocketIO/) - to add `WebSocket` support to a Flask app
---
template: useful-modules
## Storing secret credentials in .env file
It is common to store sensitive information -- such as a database connection string, usernames, passwords needed by the back-end -- in a file named `.env` in the same directory as the Python script. This file should be excluded from version control by adding it to the [.gitignore](../git-and-github/configuration) file.
--
For example:
```bash
MONGO_DBNAME=example
MONGO_URI="mongodb://admin:secret@localhost:27017"
```
---
template: useful-modules
## Storing secret credentials in .env file (continued)
The python package named [python-dotenv](https://pypi.org/project/python-dotenv/) can be used to easily load these variables into program memory:
--
For example:
```python
from dotenv import load_dotenv
# load the variables from the .env file
load_dotenv()
MONGO_DBNAME = os.getenv('MONGO_DBNAME')
MONGO_URI = os.genenv('MONGO_URI')
```
---
template: useful-modules
## User authentication
The python package named [flask-login](https://pypi.org/project/Flask-Login/) can be used to manage user sessions in a Flask app.
---
name: conclusions
# Conclusions
Thank you. Bye