Skip to content

Instantly share code, notes, and snippets.

@PaulSayantan
Created September 5, 2020 09:54
Show Gist options
  • Save PaulSayantan/de0367898b194766fe3bf0be4944f81b to your computer and use it in GitHub Desktop.
Save PaulSayantan/de0367898b194766fe3bf0be4944f81b to your computer and use it in GitHub Desktop.
Flask Tutorial templates
from flask import Flask, jsonify, request, url_for, redirect, render_template, session
app = Flask(__name__)
app.config['DEBUG'] = True
app.config['SECRET_KEY'] = '123bkh123h1v2hv31vhhy'
'''
------------------------------- :: SECTION 1:FLASK BASICS :: -------------------------------------
'''
# This is a route
@app.route('/')
def index():
session.pop('name', None)
return '<h1>Welcome to this course of flask</h1>'
@app.route('/home', methods=['POST', 'GET'], defaults={'name':'Sayantan Paul'})
@app.route('/home/<string:name>', methods=['POST', 'GET'])
def home(name):
session['name'] = name
return '<h1>This is a homepage. I am {}</h1>'.format(name)
@app.route('/json')
def json():
if 'name' in session:
name = session['name']
else:
name = None;
return jsonify({'key1': 'value2', 'key2': 10, 'key3': [100, 12, 15], 'name': name})
@app.route('/query')
def query():
name = request.args.get('name')
age = request.args.get('age')
location = request.args.get('location')
return """
<h1>This is a Query page made in flask</h1>
<h2>I am {} from {}</h2>
<h2>My age is {}</h2>
""".format(name, location, age)
@app.route('/form')
def theform():
return '''
<h1> This is a Form made in Flask</h1>
<form method="POST" action="/process">
NAME:<input type="text" name="name"><br>
AGE:<input type="text" name="age"><br>
LOCATION:<input type="text" name="location"><br>
<input type="Submit">
</form>
'''
@app.route('/process', methods=['POST'])
def process():
name = request.form['name']
age = request.form['age']
location = request.form['location']
return jsonify({
'Name': name,
'Age': age,
'Location': location
})
@app.route('/processjson', methods=['POST', 'GET'])
def processjson():
data = request.get_json()
name = data['Name']
age = data['Age']
likes = data['Likes']
return '''
<h1>Hi! I am {}</h1>
<h2> My age is {}</h2>
<h2> I like {}</h2>
'''.format(name, age, ' '.join(map(str, likes)))
@app.route('/processform', methods=['GET', 'POST'])
def processform():
if request.method == 'GET':
return '''
<h1> This is a Form made in Flask</h1>
<form method="POST" action="/processform">
NAME:<input type="text" name="name"><br>
AGE:<input type="text" name="age"><br>
LOCATION:<input type="text" name="location"><br>
<input type="Submit" value="Submit">
</form>
'''
else:
name = request.form['name']
age = request.form['age']
location = request.form['location']
return '''
<h1>Hi! I am {}</h1>
<h2> My age is {}</h2>
<h2> I live in {}</h2>
'''.format(name, age, location)
@app.route('/thanks/<string:n>', methods=['GET', 'POST'])
def thanks(n):
return '''
<h1>Thanks, {} for submitting the form.</h1>
'''.format(n)
@app.route('/redirect', methods=['GET', 'POST'])
def redirecting():
if request.method == 'GET':
return '''
<h1> This is a Form made in Flask</h1>
<form method="POST" action="/redirect">
NAME: <input type="text" name="name"><br>
AGE: <input type="text" name="age"><br>
LOCATION: <input type="text" name="location"><br>
<input type="Submit" value="Submit">
</form>
'''
else:
name = request.form['name']
return redirect(url_for('thanks', n=name))
'''
------------------------------- :: SECTION 2:TEMPLATES :: ---------------------------------------
'''
@app.route('/formpage')
def formpage():
return render_template('form.html')
@app.route('/homepage/<string:auth>')
def homepage(auth):
return render_template('home.html', name=auth)
@app.route('/condition')
def condition():
return render_template('conditional.html', display=False)
@app.route('/forloop')
def forloop():
name = 'Sayantan Paul'
return render_template('forloop.html', name=name,
langlist=['Python', 'Java', 'Golang', 'C', 'C++'],
mylang=[{'choice': 'Python'}, {'choice': 'Golang'}, {'choice': 'Java'}])
@app.route('/image')
def image():
return render_template('image.html')
@app.route('/windows')
def windows():
return render_template('windows.html')
if __name__ == '__main__':
app.run(debug=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment