# Intro to Flask Part 2

#### Dynamic Routes

```python
@app.route('/hello/<name>)
def hello(name):
    return f'Hello {name}
```

#### Returning HTML from Flask

You can either return HTML as a string like so:

```python
@app.route('/hello')
def hello_world():
    return """
<!DOCTYPE html>
<html>
  <head>
  </head>
  <body>
    <h1>Hello there.</h1>
    <p>Isn't this lovely!</p>
  </body>
</html>
"""
```

or use a template.

#### Serving Dynamic Templates & Static Files

Create `static` and `templates` directories so the project directory looks like so:

```
.
├── app.py
├── static
│   ├── css
│   ├── img
│   └── js
└── templates
```

**Rendering a Template**

Flask will automatically look for template files in `templates`.

Flask uses the [Jinja templating engine](https://jinja.palletsprojects.com/en/3.1.x/).

To render a Jinja template, first import the `render_template` function

```python
from flask import Flask, render_template
```

Then instead of returning a string in the route handler, return the result of calling `render_template('{templatename}.html')`.

**app.py**

```python
@app.route('/')
def index():
    return render_template('index.html')
```

**templates/index.html**

```html
<ul>
  <li><a href="/hello/bon">bon</a></li>
  <li><a href="/hello/brendan">brendan</a></li>
  <li><a href="/hello/dave">dave</a></li>
  <li><a href="/hello/jason">jason</a></li>
  <li><a href="/hello/jeremy">jeremy</a></li>
  <li><a href="/hello/justin">justin</a></li>
  <li><a href="/hello/michael">michael</a></li>
  <li><a href="/hello/mun">mun</a></li>
  <li><a href="/hello/renata">renata</a></li>
  <li><a href="/hello/shiam">shiam</a></li>
  <li><a href="/hello/tania">tania</a></li>
  <li><a href="/hello/tristram">tristram</a></li>
  <li><a href="/hello/xin">xin</a></li>
  <li><a href="/hello/yuki">yuki</a></li>
</ul>
```

**Adding a Stylesheet**

Like templates, Flask will automatically look for and and serve static files in `static`. So you just have to create your stylesheet file and include it in the template.

```html
<link href="/static/css/styles.css" rel="stylesheet">
```

**Adding Images**

Add any image in the `static` directory (preferably in the `img` directory but you do you) and flask will serve it at `/static/PATH_TO_IMAGE`.

```html
<img src="/static/img/dog.jpg" alt="Good Boy" />
```

#### Passing Data to Templates

**app.py**

```python
@app.route('/hello/<name>')
def hello(name):
    return render_template('hello.html', person=name)
```

**templates/hello.html**

```html
<h1>Hello {{ person }}!</h1>
<p>Have you caught up with homework?</p>
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ga0-2.gitbook.io/seifxr10anz-content/week-7/day-3-functions-http-flask/intro-to-flask-part-2.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
