Spaces:
Runtime error
Runtime error
Initial commit
Browse files- Dockerfile +6 -0
- app.py +21 -0
- docker-compose.yml +22 -0
- index.html +22 -0
- requirements.txt +2 -0
- test.sql +9 -0
Dockerfile
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.8-slim
|
| 2 |
+
COPY . /app
|
| 3 |
+
WORKDIR /app
|
| 4 |
+
RUN pip install -r requirements.txt
|
| 5 |
+
ENTRYPOINT ["flask"]
|
| 6 |
+
CMD ["run", "--host=0.0.0.0"]
|
app.py
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, render_template
|
| 2 |
+
import pymysql
|
| 3 |
+
|
| 4 |
+
app = Flask(__name__)
|
| 5 |
+
|
| 6 |
+
db = pymysql.connect(
|
| 7 |
+
host="db",
|
| 8 |
+
user="user",
|
| 9 |
+
password="password",
|
| 10 |
+
database="mysqldb"
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
+
@app.route("/")
|
| 14 |
+
def index():
|
| 15 |
+
cursor = db.cursor()
|
| 16 |
+
cursor.execute("SELECT * FROM users")
|
| 17 |
+
data = cursor.fetchall()
|
| 18 |
+
return render_template("index.html", data=data)
|
| 19 |
+
|
| 20 |
+
if __name__ == "__main__":
|
| 21 |
+
app.run(host="0.0.0.0", port=5000)
|
docker-compose.yml
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version: "3"
|
| 2 |
+
services:
|
| 3 |
+
web:
|
| 4 |
+
build: .
|
| 5 |
+
ports:
|
| 6 |
+
- "5000:5000"
|
| 7 |
+
depends_on:
|
| 8 |
+
- db
|
| 9 |
+
environment:
|
| 10 |
+
- FLASK_APP=app.py
|
| 11 |
+
- FLASK_ENV=development
|
| 12 |
+
db:
|
| 13 |
+
image: mysql/mysql-server
|
| 14 |
+
ports:
|
| 15 |
+
- "3306:3306"
|
| 16 |
+
environment:
|
| 17 |
+
- MYSQL_DATABASE=mysqldb
|
| 18 |
+
- MYSQL_USER=user
|
| 19 |
+
- MYSQL_PASSWORD=password
|
| 20 |
+
- MYSQL_ROOT_PASSWORD=password
|
| 21 |
+
volumes:
|
| 22 |
+
- ./test.sql:/docker-entrypoint-initdb.d/test.sql
|
index.html
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<html>
|
| 2 |
+
<head>
|
| 3 |
+
<title>Flask App with MySQL</title>
|
| 4 |
+
</head>
|
| 5 |
+
<body>
|
| 6 |
+
<h1>Users</h1>
|
| 7 |
+
<table>
|
| 8 |
+
<tr>
|
| 9 |
+
<th>ID</th>
|
| 10 |
+
<th>Name</th>
|
| 11 |
+
<th>Email</th>
|
| 12 |
+
</tr>
|
| 13 |
+
{% for row in data %}
|
| 14 |
+
<tr>
|
| 15 |
+
<td>{{ row[0] }}</td>
|
| 16 |
+
<td>{{ row[1] }}</td>
|
| 17 |
+
<td>{{ row[2] }}</td>
|
| 18 |
+
</tr>
|
| 19 |
+
{% endfor %}
|
| 20 |
+
</table>
|
| 21 |
+
</body>
|
| 22 |
+
</html>
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Flask==2.0.2
|
| 2 |
+
pymysql==1.0.2
|
test.sql
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
CREATE TABLE users (
|
| 2 |
+
id INT PRIMARY KEY,
|
| 3 |
+
name VARCHAR(255),
|
| 4 |
+
email VARCHAR(255)
|
| 5 |
+
);
|
| 6 |
+
|
| 7 |
+
INSERT INTO users VALUES (1, 'Alice', 'alice@example.com');
|
| 8 |
+
INSERT INTO users VALUES (2, 'Bob', 'bob@example.com');
|
| 9 |
+
INSERT INTO users VALUES (3, 'Charlie', 'charlie@example.com');
|