Spaces:
Runtime error
Runtime error
Upload 5 files
Browse files- app.py +47 -0
- docker-compose.yml +15 -0
- requirements.txt +3 -0
- templates/dashboard.html +84 -0
- templates/login.html +72 -0
app.py
ADDED
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, render_template, request, redirect, url_for, session
|
2 |
+
from flask_mysqldb import MySQL
|
3 |
+
|
4 |
+
app = Flask(__name__)
|
5 |
+
app.config['MYSQL_HOST'] = 'mysql'
|
6 |
+
app.config['MYSQL_USER'] = 'root'
|
7 |
+
app.config['MYSQL_PASSWORD'] = 'password'
|
8 |
+
app.config['MYSQL_DB'] = 'login_demo'
|
9 |
+
|
10 |
+
mysql = MySQL(app)
|
11 |
+
|
12 |
+
@app.route('/')
|
13 |
+
def index():
|
14 |
+
if 'username' in session:
|
15 |
+
username = session['username']
|
16 |
+
return f'Logged in as {username} | <a href="/logout">Logout</a> | <a href="/dashboard">Dashboard</a>'
|
17 |
+
return 'You are not logged in | <a href="/login">Login</a>'
|
18 |
+
|
19 |
+
@app.route('/login', methods=['GET', 'POST'])
|
20 |
+
def login():
|
21 |
+
if request.method == 'POST':
|
22 |
+
username = request.form['username']
|
23 |
+
password = request.form['password']
|
24 |
+
cursor = mysql.connection.cursor()
|
25 |
+
cursor.execute('SELECT * FROM users WHERE username = %s AND password = %s', (username, password))
|
26 |
+
user = cursor.fetchone()
|
27 |
+
cursor.close()
|
28 |
+
if user:
|
29 |
+
session['username'] = username
|
30 |
+
return redirect(url_for('index'))
|
31 |
+
else:
|
32 |
+
return 'Login failed. Invalid username or password.'
|
33 |
+
return render_template('login.html')
|
34 |
+
|
35 |
+
@app.route('/dashboard')
|
36 |
+
def dashboard():
|
37 |
+
if 'username' in session:
|
38 |
+
return render_template('dashboard.html', username=session['username'])
|
39 |
+
return redirect(url_for('login'))
|
40 |
+
|
41 |
+
@app.route('/logout')
|
42 |
+
def logout():
|
43 |
+
session.pop('username', None)
|
44 |
+
return redirect(url_for('index'))
|
45 |
+
|
46 |
+
if __name__ == '__main__':
|
47 |
+
app.run(debug=True, host='0.0.0.0')
|
docker-compose.yml
ADDED
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
version: '3'
|
2 |
+
|
3 |
+
services:
|
4 |
+
web:
|
5 |
+
build: .
|
6 |
+
ports:
|
7 |
+
- "5000:5000"
|
8 |
+
depends_on:
|
9 |
+
- mysql
|
10 |
+
mysql:
|
11 |
+
image: mysql:8.0
|
12 |
+
environment:
|
13 |
+
MYSQL_ROOT_PASSWORD: password
|
14 |
+
volumes:
|
15 |
+
- ./init.sql:/docker-entrypoint-initdb.d/init.sql
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
Flask==2.0.2
|
2 |
+
Werkzeug==2.0.2
|
3 |
+
Flask-MySQLdb==0.2.0
|
templates/dashboard.html
ADDED
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<!DOCTYPE html>
|
2 |
+
<html lang="en">
|
3 |
+
<head>
|
4 |
+
<meta charset="utf-8">
|
5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
6 |
+
<title>Dashboard</title>
|
7 |
+
<style>
|
8 |
+
body {
|
9 |
+
font-family: Arial, sans-serif;
|
10 |
+
background-color: #f4f4f4;
|
11 |
+
margin: 0;
|
12 |
+
padding: 0;
|
13 |
+
}
|
14 |
+
|
15 |
+
header {
|
16 |
+
background-color: #333333;
|
17 |
+
color: #ffffff;
|
18 |
+
padding: 10px;
|
19 |
+
text-align: center;
|
20 |
+
}
|
21 |
+
|
22 |
+
nav {
|
23 |
+
background-color: #4caf50;
|
24 |
+
padding: 10px;
|
25 |
+
color: #ffffff;
|
26 |
+
text-align: center;
|
27 |
+
}
|
28 |
+
|
29 |
+
main {
|
30 |
+
padding: 20px;
|
31 |
+
}
|
32 |
+
|
33 |
+
.card {
|
34 |
+
background-color: #ffffff;
|
35 |
+
padding: 20px;
|
36 |
+
border-radius: 8px;
|
37 |
+
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
|
38 |
+
margin-bottom: 20px;
|
39 |
+
}
|
40 |
+
|
41 |
+
.card h2 {
|
42 |
+
color: #333333;
|
43 |
+
}
|
44 |
+
|
45 |
+
.card p {
|
46 |
+
color: #666666;
|
47 |
+
}
|
48 |
+
|
49 |
+
@media only screen and (max-width: 600px) {
|
50 |
+
.card {
|
51 |
+
width: 100%;
|
52 |
+
}
|
53 |
+
}
|
54 |
+
</style>
|
55 |
+
</head>
|
56 |
+
<body>
|
57 |
+
<header>
|
58 |
+
<h1>Dashboard</h1>
|
59 |
+
</header>
|
60 |
+
|
61 |
+
<nav>
|
62 |
+
<a href="#">Home</a> |
|
63 |
+
<a href="#">Profile</a> |
|
64 |
+
<a href="#">Settings</a>
|
65 |
+
</nav>
|
66 |
+
|
67 |
+
<main>
|
68 |
+
<div class="card">
|
69 |
+
<h2>Welcome to Your Dashboard</h2>
|
70 |
+
<p>This is a simple dashboard page with some content.</p>
|
71 |
+
</div>
|
72 |
+
|
73 |
+
<div class="card">
|
74 |
+
<h2>Recent Activity</h2>
|
75 |
+
<p>No recent activity to display.</p>
|
76 |
+
</div>
|
77 |
+
|
78 |
+
<div class="card">
|
79 |
+
<h2>Statistics</h2>
|
80 |
+
<p>No statistics available.</p>
|
81 |
+
</div>
|
82 |
+
</main>
|
83 |
+
</body>
|
84 |
+
</html>
|
templates/login.html
ADDED
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<!doctype html>
|
2 |
+
<html lang="en">
|
3 |
+
<head>
|
4 |
+
<meta charset="utf-8">
|
5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
6 |
+
<title>Login Page</title>
|
7 |
+
<style>
|
8 |
+
body {
|
9 |
+
font-family: Arial, sans-serif;
|
10 |
+
background-color: #f4f4f4;
|
11 |
+
margin: 0;
|
12 |
+
padding: 0;
|
13 |
+
display: flex;
|
14 |
+
justify-content: center;
|
15 |
+
align-items: center;
|
16 |
+
height: 100vh;
|
17 |
+
}
|
18 |
+
|
19 |
+
form {
|
20 |
+
background-color: #ffffff;
|
21 |
+
padding: 20px;
|
22 |
+
border-radius: 8px;
|
23 |
+
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
|
24 |
+
width: 300px;
|
25 |
+
}
|
26 |
+
|
27 |
+
h2 {
|
28 |
+
text-align: center;
|
29 |
+
color: #333333;
|
30 |
+
}
|
31 |
+
|
32 |
+
label {
|
33 |
+
display: block;
|
34 |
+
margin-bottom: 8px;
|
35 |
+
color: #333333;
|
36 |
+
}
|
37 |
+
|
38 |
+
input {
|
39 |
+
width: 100%;
|
40 |
+
padding: 8px;
|
41 |
+
margin-bottom: 16px;
|
42 |
+
box-sizing: border-box;
|
43 |
+
}
|
44 |
+
|
45 |
+
input[type="submit"] {
|
46 |
+
background-color: #4caf50;
|
47 |
+
color: #ffffff;
|
48 |
+
cursor: pointer;
|
49 |
+
}
|
50 |
+
|
51 |
+
input[type="submit"]:hover {
|
52 |
+
background-color: #45a049;
|
53 |
+
}
|
54 |
+
|
55 |
+
@media only screen and (max-width: 600px) {
|
56 |
+
form {
|
57 |
+
width: 80%;
|
58 |
+
}
|
59 |
+
}
|
60 |
+
</style>
|
61 |
+
</head>
|
62 |
+
<body>
|
63 |
+
<form method="post" action="/login">
|
64 |
+
<h2>Login</h2>
|
65 |
+
<label for="username">Username:</label>
|
66 |
+
<input type="text" id="username" name="username" required>
|
67 |
+
<label for="password">Password:</label>
|
68 |
+
<input type="password" id="password" name="password" required>
|
69 |
+
<input type="submit" value="Login">
|
70 |
+
</form>
|
71 |
+
</body>
|
72 |
+
</html>
|