8000 Working on Flask (getting familiar) · code93/trantor-bootcamp@3e8295f · GitHub
[go: up one dir, main page]

Skip to content

Commit 3e8295f

Browse files
committed
Working on Flask (getting familiar)
Created a Flask App for Registration using Python while implemented sqlite3 library to connect and manipulate database.
1 parent bc653c6 commit 3e8295f

File tree

9 files changed

+125
-0
lines changed

9 files changed

+125
-0
lines changed
1.42 KB
Binary file not shown.

Project/Flask APP/app.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import pandas as pd
2+
import sqlite3
3+
from flask import Flask, redirect, render_template, request
4+
5+
6+
app = Flask(__name__)
7+
8+
conn=sqlite3.connect("froshims.db",check_same_thread=False)
9+
cur= conn.cursor()
10+
cur.execute('''CREATE TABLE IF NOT EXISTS registrants(name TEXT, sport TEXT)''')
11+
conn.commit()
12+
13+
df = pd.read_sql_query("SELECT * FROM registrants",con=conn)
14+
15+
SPORTS = [
16+
"Dodgeball",
17+
"Flag Football",
18+
"Soccer",
19+
"Volleyball",
20+
"Ultimate Frisbee"
21+
]
22+
23+
@app.route("/")
24+
def index():
25+
return render_template("index.html", sports=SPORTS)
26+
27+
28+
@app.route("/register", methods=["POST"])
29+
def register():
30+
31+
name = request.form.get("name")
32+
if not name:
33+
return render_template("error.html", message="Missing name")
34+
35+
sport = request.form.get("sport")
36+
if not sport:
37+
return render_template("error.html", message="Missing sport")
38+
if sport not in SPORTS:
39+
return render_template("error.html", message="Invalid sport")
40+
41+
df = (name,sport)
42+
43+
cur.execute('''INSERT INTO registrants(name,sport) VALUES(?,?)''', df)
44+
45+
return redirect("/registrants")
46+
47+
48+
@app.route("/registrants")
49+
def registrants():
50+
registrants = cur.execute('''SELECT * FROM registrants''')
51+
return render_template("registrants.html", registrants=registrants)

Project/Flask APP/froshims.db

8 KB
Binary file not shown.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{% extends "layout.html" %}
2+
3+
{% block body %}
4+
5+
{{ message }}
6+
7+
{% endblock %}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{% extends "layout.html" %}
2+
3+
{% block body %}
4+
5+
You are not Registered
6+
7+
{% endblock %}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{% extends "layout.html" %}
2+
3+
{% block body %}
4+
<h1>Register</h1>
5+
6+
<form action="/register" method="post">
7+
8+
<input autocomplete="off" autofocus name="name" placeholder="Name" type="text">
9+
<select name="sport">
10+
<option disabled selected value="">Sport</option>
11+
{% for sport in sports %}
12+
<option value="{{ sport }}">{{ sport }}</option>
13+
{% endfor %}
14+
</select>
15+
<input type="submit" value="Register">
16+
17+
18+
</form>
19+
{% endblock %}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
3+
<html lang="en">
4+
<head>
5+
<meta name="viewport" content="initial-scale=1, width=device-width">
6+
<title>froshims</title>
7+
</head>
8+
<body>
9+
{% block body %}{% endblock %}
10+
</body>
11+
</html>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{% extends "layout.html" %}
2+
3+
{% block body %}
4+
5+
<h1>Registrants</h1>
6+
<table>
7+
<thead>
8+
<tr>
9+
<th>Name</th>
10+
<th>Sport</th>
11+
</tr>
12+
</thead>
13+
<tbody>
14+
{% for registrant in registrants %}
15+
<tr>
16+
<td>{{registrant[0]}}</td>
17+
<td>{{registrant[1]}}</td>
18+
</tr>
19+
{% endfor %}
20+
</tbody>
21+
</table>
22+
23+
{% endblock %}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{% extends "layout.html" %}
2+
3+
{% block body %}
4+
5+
You are Registered
6+
7+
{% endblock %}

0 commit comments

Comments
 (0)
0