# Introduction

## What is Flask?

Flask is a micro-framework for the web, written in Python.

Micro because the basis for its functionality is extremely limited, in spite of its numerous extensions.

Used for creating websites and APIs.

Flask is based on the projects Werkzeug and Jinja 2.

### Why use Flask?

* Easy to learn.
* *Pythonic* (adopts the principles and style of programming from Python).
* Small/light but scalable for big applications.
* Decorator defined routes

## Development

### Environment creation

```bash
$ mkdir project
$ cd project
$ mkdir static
$ mkdir templates
$ touch app.py
$ chmod +x app.py
$ python3 -m venv venv
$ source venv/bin/activate
$ pip install Flask
```

### Routing/Redirect

```python
from flask import redirect, url_for

@app.route("/user/<name>")
def say_hi(name):
    return f"Hi {name}"
    
@app.route("/index.html")
def index2():
    return redirect(url_for('index')) # index is the name of a function
```

### Static

Files in the *static* directory are served directly (without the need for any code):

* Useful to place images, JavaScript, CSS, etc.

### Jinja2 templates

#### hello.html

```php
<!doctype html>HP
<title>Hello</title>
{% if name %}
    <h1>Hello {{ name  }}</h1>
{% else %}
    <h1>Hello World</h1>
{% endif %}
```

```python
from flask import Flask, render_template

app = Flask(__name__)

@app.route("/user")
@app.route("/user/<name>")
def say_hi(name=None):
    return render_template("hello.html", name=name)

if __name__ == "__main__":
    app.run()
```

#### users.html

```php
<!doctype html> 
<title>Hello</title> 
{% for user in users %}
    <h1>Hello {{ user }}</h1>
{% endfor %}
```

```python
from flask import Flask, render_template

app = Flask(__name__)

@app.route("/users")
def list_users():
    users = [“Diogo”, “Gomes”]
    return render_template(”users.html", users=users)
    
if __name__ == "__main__":
    app.run()
```

### HTTP methods

```python
@app.route("/login", methods=['GET', 'POST'])
def login():
    if request.method == 'POST':
        return f"Welcome { request.form['username'] }"
    else:
        return "curl -X POST -F 'username=dgomes' http://localhost:5000/login"
```

#### Receive a file through a POST request

```python
@app.route('/upload', methods=['POST'])
def upload_file():
    file = request.files['file']
    file.save(file.filename)
    return f"Thank you"
```


---

# 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://davidjosearaujo.gitbook.io/notes-miect/computacao-distribuida/flask/introduction.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.
