Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Complete Flask toolbox #4

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion flask_app.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,41 @@
"""
Put your Flask app code here.
"""
"""
from flask import Flask, render_template, request

app = Flask(__name__)

@app.route('/login', methods=['GET', 'POST'])
def login():
error = None
if request.form['name'] and request.form['age'] and request.form['ninja']: # if all inputs are given
name = request.form['name']
age = request.form['age']
ninja = request.form['ninja']
if check_exist(ninja): # if NINJA choice valid
return render_template('profile.html', name = name, age = age)
else: # redirect to hello page if credentials invalid
error = 'Invalid Entry'
return render_template('profile.html',name = name, age = age, error=error)
else:
return '''
<h1>Error: Did not include all required info !</h1>

<form action="/" method="GET">
<input type="submit" value="Return to Home">
</form>
'''

def check_exist(ninja):
ninjas = ['Emily', 'Duncan', 'Hannah', 'Matt', 'Seungin', 'Tony']
if ninja in ninjas:
return True
else:
return False

@app.route('/')
def main():
return render_template('hello.html')

if __name__ == '__main__':
app.run()
30 changes: 27 additions & 3 deletions hello.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,35 @@
"""

from flask import Flask
from flask import render_template

app = Flask(__name__)

@app.route('/')
def hello_world():
return 'Hello World!'
def login():
if request.method == 'POST':
check_exist()
else:
show_the_login_form()

@app.route('/play')
@app.route('/hello/<string:name>/')
def hello(name=None):
return render_template('hello.html', name = name)

@app.route('/user/<username>')
def show_user_profile(username):
# show the user profile for that user
return 'User %s' % username

@app.route('/post/<int:post_id>')
def show_post(post_id):
# show the post with the given id, the id is an integer
return 'Post %d' % post_id
def check_exist():
pass

def show_the_login_form():
pass

if __name__ == '__main__':
app.run()
12 changes: 12 additions & 0 deletions index
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html>
<head>
<title>
A Small Hello
</title>
</head>
<body>
<h1>Hi</h1>
<p>This is very minimal "hello world" HTML document.</p>
</body>
</html>
16 changes: 16 additions & 0 deletions templates/hello.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!doctype html>

<head>
<title>Hello from Flask</title>
<h1>Hello World!</h1>
</head>

<body>
<form action="/login" method="POST">
Name: <input type="text" name="name"><br><br>
Age: <input type="int" name="age"><br><br>
Favorite SoftDes NINJA: <input type="text" name="ninja"><br><br>
<input type="submit" value="Submit">
</form>

</body>
12 changes: 12 additions & 0 deletions templates/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html>
<head>
<title>
A Small Hello
</title>
</head>
<body>
<h1>Hi</h1>
<p>This is very minimal "hello world" HTML document.</p>
</body>
</html>
20 changes: 20 additions & 0 deletions templates/profile.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!doctype html>

<head>
<title>Flask</title>
<h1> Your Profile </h1>
</head>

<body>
{% if error %}
<h3>Error: {{ error }}!</h3>
{% endif %}

<p> Name: {{name}} </p>
<p> Age: {{age}} </p>
<p> Favorite NINJA: Patrick Huston </p>

<form action="/" method="GET">
<input type="submit" value="Return to Home">
</form>
</body>