Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/julioaranajr/08_flask.md
Python Flask move app In this exercise we will create python Flask app that will query data from your MySQL database created in previous exercise
https://github.com/julioaranajr/08_flask.md
docker flask flask-application localhost mysql mysql-database python-script python3 website
Last synced: 13 days ago
JSON representation
Python Flask move app In this exercise we will create python Flask app that will query data from your MySQL database created in previous exercise
- Host: GitHub
- URL: https://github.com/julioaranajr/08_flask.md
- Owner: julioaranajr
- Created: 2022-10-02T17:57:04.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2022-10-13T19:25:49.000Z (over 2 years ago)
- Last Synced: 2024-11-11T08:50:00.989Z (2 months ago)
- Topics: docker, flask, flask-application, localhost, mysql, mysql-database, python-script, python3, website
- Homepage:
- Size: 2.93 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Flask
### Python Flask move app
In this exercise we will create python Flask app that will query data from your MySQL database created in previous exercise.
Make sure your mysql docker container is running, as otherwise there is nothing to connect to.Create new directory for the project
```sh
mkdir ~/src/Talent-Academy/flask-app
```Inside flask-app directory create new source file `movie_app.py`
```python
import jsonfrom flask import Flask, render_template
from flask_mysqldb import MySQLapp = Flask("MovieApp")
app.config["MYSQL_HOST"] = "127.0.0.1"
app.config["MYSQL_USER"] = "root"
app.config["MYSQL_PASSWORD"] = "" # TODO enter your supersecret password
app.config["MYSQL_DB"] = "movie_db"mysql = MySQL(app)
@app.route("/")
# Just to test that flask is working with Hello World
def hello_world():
return "Hello World!
"@app.route("/movies/")
def list_movies():
cursor = mysql.connection.cursor()
query_string = "SELECT * FROM movies_tbl;"
cursor.execute(query_string)
data = cursor.fetchall()
cursor.close()
return json.dumps(data) # Return data as a string@app.route("/movies-table/")
def list_movie_table():
cursor = mysql.connection.cursor()
query_string = "SELECT * FROM movies_tbl;"
cursor.execute(query_string)
data = cursor.fetchall()
cursor.close()
return render_template("movies.html.tpl", movies_data=data) # Return data as rendered html templateif __name__ == "__main__":
app.run(host="127.0.0.1")```
### Jinja2 html template
First create `templates` folder for templates, this is the default location for Flask
```sh
mkdir ~/src/Talent-Academy/flask-app/templates
```Inside `templates` folder create a new Jinja2 template file `movies.html.tpl`
```html
Title
Year{% for movie in movies_data %}
{{movie[1]}}
{{movie[2]}}{% endfor %}
```
### Run Flask app
```sh
python movie_app.py
```You can test your page in the web browser, e.g. http://127.0.0.1:5000/movies-table/