Skip to content

Instantly share code, notes, and snippets.

@arnavsawant9
Last active April 27, 2025 10:58
Show Gist options
  • Save arnavsawant9/7271425e9636b3d5c555dd1cbfbb1a4b to your computer and use it in GitHub Desktop.
Save arnavsawant9/7271425e9636b3d5c555dd1cbfbb1a4b to your computer and use it in GitHub Desktop.
Flask
#Using Flask, create a basic web application with two routes: The root route (/) should display a welcome message. Another route (/about) should display a short "About Us" message.
from flask import Flask
app = Flask(__name__)
@app.route('/')
def home():
return "Welcome to our Flask web application!"
@app.route('/about')
def about():
return "About Us: We are building awesome web apps with Flask!"
if __name__ == '__main__':
app.run(debug=True)
#Create a Flask application with a route /hello that accepts a name as a query parameter (GET method). Example: visiting /hello?name=Raj should display “Hello, Raj!” on the page.
# remember to do http://127.0.0.1:5000/hello?name=Raj
from flask import Flask, request
app = Flask(__name__)
@app.route('/hello')
def hello():
name = request.args.get('name', 'Guest') # Default to 'Guest' if no name provided
return f"Hello, {name}!"
if __name__ == '__main__':
app.run(debug=True)
#Create a Flask application with a route /form that displays a form to collect user name and age (using GET). When the form is submitted (POST), show a message like: "Thank you!"
#app.py
from flask import Flask, request, render_template
app = Flask(__name__)
@app.route('/form', methods=['GET', 'POST'])
def form():
if request.method == 'POST':
return render_template('thank_you.html')
return render_template('form.html')
if __name__ == '__main__':
app.run(debug=True)
#templates/form.html
<!doctype html>
<html>
<head><title>User Form</title></head>
<body>
<form method="POST">
Name: <input type="text" name="name" required><br>
Age: <input type="number" name="age" required><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
#templates/thank_you.html
<!doctype html>
<html>
<head><title>Thank You</title></head>
<body>
<h2>Thank you!</h2>
</body>
</html>
#Create a simple Flask app with a /login route that displays a login form with username and password fields. On submitting (POST), check if the username is admin and password is 1234, then display “Login Successful”.
#app.py
from flask import Flask, request, render_template
app = Flask(__name__)
@app.route('/login', methods=['GET', 'POST'])
def login():
message = ''
if request.method == 'POST':
username = request.form.get('username')
password = request.form.get('password')
if username == 'admin' and password == '1234':
message = 'Login Successful'
else:
message = 'Invalid credentials'
return render_template('login.html', message=message)
if __name__ == '__main__':
app.run(debug=True)
#templates/login.html
<!doctype html>
<html>
<head><title>Login</title></head>
<body>
<h2>Login Form</h2>
<form method="POST">
Username: <input type="text" name="username" required><br>
Password: <input type="password" name="password" required><br>
<input type="submit" value="Login">
</form>
<p>{{ message }}</p>
</body>
</html>
#Create a Flask app with a route /home that renders a static HTML page named home.html using the render_template() function.
#app.py
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/home')
def home():
return render_template('home.html')
if __name__ == '__main__':
app.run(debug=True)
#templates/home.html
<!doctype html>
<html>
<head>
<title>Home Page</title>
</head>
<body>
<h1>Welcome to the Home Page!</h1>
<p>This is a static HTML page served by Flask.</p>
</body>
</html>
#Design a Flask application with a route /hello/<name>. Pass the name variable from the route to an HTML template using render_template().
#app.py
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/hello/<name>')
def hello(name):
return render_template('hello.html', user_name=name)
@app.route('/')
def index():
return "hello this is the home page just go to /hello/<name> to see the hello page"
if __name__ == '__main__':
app.run(debug=True)
#templates/hello.html
<!doctype html>
<html>
<head>
<title>Hello Page</title>
</head>
<body>
<h1>Hello, {{ user_name }}!</h1>
<p>Welcome to our Flask app.</p>
</body>
</html>
# Create a Flask app with a route /students that passes a list of student names to an HTML template. Use a for loop in the template to display the list in bullet points.
#app.py
from flask import Flask, render_template, request
app = Flask(__name__)
@app.route('/students')
def students():
student_list = ["Arnav", "Siddhant", "Pranav Titambe", "Pranav Pol"]
return render_template('students.html', students=student_list)
if __name__ == '__main__':
app.run(debug=True)
#templates/students.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>students list</title>
</head>
<body>
<h1>Students list</h1>
<ul>
{% for student in students %}
<li>{{student}}</li>
{% endfor %}
</ul>
</body>
</html>
# Build a Flask app with a route /feedback that displays a form using an HTML template. When the user submits their name and feedback (POST), show the submitted data below the form on the same page.
#app.py
from flask import Flask, request, render_template
app = Flask(__name__)
@app.route('/feedback', methods=['GET', 'POST'])
def feedback():
name = ''
comment = ''
if request.method == 'POST':
name = request.form.get('name')
comment = request.form.get('feedback')
return render_template('feedback.html', name=name, comment=comment)
if __name__ == '__main__':
app.run(debug=True)
#templates/feedback.html
<!doctype html>
<html>
<head>
<title>Feedback Form</title>
</head>
<body>
<h1>Feedback Form</h1>
<form method="POST">
Name: <input type="text" name="name" required><br><br>
Feedback:<br>
<textarea name="feedback" rows="4" cols="40" required></textarea><br><br>
<input type="submit" value="Submit">
</form>
{% if name and comment %}
<h2>Submitted Feedback</h2>
<p><strong>Name:</strong> {{ name }}</p>
<p><strong>Feedback:</strong> {{ comment }}</p>
{% endif %}
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment