Last active
March 12, 2020 14:28
-
-
Save njdart/38d5889541f0ad99fcd7b85c07f5d6ae to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask | |
app = Flask(__name__) | |
@app.route('/') | |
def hello_world(): | |
return 'Hello, World!' | |
@app.route('/about') | |
def about(): | |
a = 5 | |
b = 16 | |
return """ | |
<html> | |
<head> | |
<title>Hello World - About</title> | |
</head> | |
<body> | |
<h1>About</h1> | |
<p>This is a simple HTML about page.</p> | |
<h2>Some simple math</h2> | |
<pre>{a} + {b} = {add}</pre> | |
<pre>{a} - {b} = {sub}</pre> | |
<pre>{a} * {b} = {mul}</pre> | |
<pre>{a} / {b} = {div}</pre> | |
</body> | |
</html> | |
""".format(a=a, b=b, add=a+b, sub=a-b, mul=a*b, div=a/b) | |
app.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# A hello world flask example. | |
# DO NOT NAME THIS FILE "flask.py". You will get the following error: | |
# cannot import name 'Flask' from partially initialized module 'flask' | |
# | |
# Also, don't forget to pip install flask | |
# | |
from flask import Flask | |
# we define a name (variable) called app that will contain your "web server" application. The Flask(..) part is how | |
# we start using the flask library. This creates a flask object (we haven't covered objects/classes) that lets us | |
# control the web server and tell it how to respond to requests (think back to the API lesson) | |
# | |
# For more info on Python Classes, See https://docs.python.org/3/tutorial/classes.html | |
# For more info on how to use flask, See https://flask.palletsprojects.com/en/1.1.x/ | |
app = Flask(__name__) | |
# We define a function called "hello_world". This should look familiar to you. The @app.route('/') above the function | |
# is new to us. This is an annotation that tells our app (our flask web server) that requests for '/' should call | |
# the "hello_world" function (we call this routing) to create the response | |
# | |
# We use '/' here, but you can replace it with other strings, such as '/hello'. Routes usually start with a '/' | |
@app.route('/') | |
def hello_world(): | |
return 'Hello, World!' | |
# Python allows us to have multi-line strings if we start and end our strings with three quote marks (eg ''' or """) | |
# We can still call .format() on these strings as we saw in earlier lessons. | |
# See https://docs.python.org/3.8/library/string.html#format-examples for some advanced .format help | |
@app.route('/about') | |
def about(): | |
a = 5 | |
b = 16 | |
return """ | |
<html> | |
<head> | |
<title>Hello World - About</title> | |
</head> | |
<body> | |
<h1>About</h1> | |
<p>This is a simple HTML about page.</p> | |
<h2>Some simple math</h2> | |
<pre>{a} + {b} = {add}</pre> | |
<pre>{a} - {b} = {sub}</pre> | |
<pre>{a} * {b} = {mul}</pre> | |
<pre>{a} / {b} = {div}</pre> | |
</body> | |
</html> | |
""".format(a=a, b=b, add=a+b, sub=a-b, mul=a*b, div=a/b) | |
# If you look at the official "flask getting started" guide, they do not include the following and instead use some | |
# "magic" to start the flask web server. Instead you can call app.run() to start it. | |
app.run() | |
# When you start this program "with python hello-flask.py", you should see the follwing output | |
## * Serving Flask app "hello-flask" (lazy loading) | |
## * Environment: production | |
## WARNING: This is a development server. Do not use it in a production deployment. | |
## Use a production WSGI server instead. | |
## * Debug mode: off | |
## * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit) | |
# | |
# When you visit http://127.0.0.1:5000/ in your browser (or http://127.0.0.1:5000/about to see the about page), | |
# you should see flask "logs" the request and response in the console: | |
## 127.0.0.1 - - [12/Mar/2020 13:22:12] "GET / HTTP/1.1" 200 - | |
## 127.0.0.1 - - [12/Mar/2020 13:22:15] "GET /about HTTP/1.1" 200 - | |
# | |
# This tells you a bunch of things: | |
# 127.0.0.1 - - [12/Mar/2020 13:22:12] "GET /about HTTP/1.1" 200 - | |
# Who - - When How What Response | |
# | |
# Who: the "identifier" of the person making the request. In this case, 127.0.0.1 (or "localhost") means the same | |
# machine as the web server is running on | |
# | |
# When: the date and time of the request | |
# | |
# How: How is the request made. This can be a couple of things, it relates to what action the request is trying to | |
# achieve. A "GET" is getting something, there are other options here to make changes ("POST", "PUT"), to get | |
# some information about something ("OPTIONS", "HEAD"), or to delete something ("DELETE"). These are called | |
# HTTP verbs (or methods). See See https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods | |
# | |
# What: What the request was for. This is the path (or route) that can be a string you defined in your @app.route(..) | |
# It could also be a path that isn't defined, for example if you mis-spelled a link, that will get logged here | |
# | |
# Response: The HTTP status code (remember back to the lession on APIs) that the web server responsed with. | |
# See https://developer.mozilla.org/en-US/docs/Web/HTTP/Status | |
# For a short overfiew, see https://twitter.com/stevelosh/status/372740571749572610 | |
# |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# A simple example that asks the user on the about page for their name, then echoes the name back to them | |
# | |
# We have to include the "request" object from flask. This lets us get information | |
# about the incoming request (such as the who, what, how, etc we saw earlier) | |
from flask import Flask, request | |
app = Flask(__name__) | |
@app.route('/') | |
def hello_world(): | |
return 'Hello, World!' | |
# We can get information from the user by adding a "<form></form>" element | |
# This has an action (a path or route to send data to), and a method (how to | |
# send the data). We can then define another flask path (or route) to do | |
# something with this data | |
@app.route('/about') | |
def about(): | |
a = 5 | |
b = 16 | |
return """ | |
<html> | |
<head> | |
<title>Hello World - About</title> | |
</head> | |
<body> | |
<h1>About</h1> | |
<p>This is a simple HTML about page.</p> | |
<h2>Some simple math</h2> | |
<pre>{a} + {b} = {add}</pre> | |
<pre>{a} - {b} = {sub}</pre> | |
<pre>{a} * {b} = {mul}</pre> | |
<pre>{a} / {b} = {div}</pre> | |
<hr /> | |
<form action="/form" method="post"> | |
<a>Please enter your name:</a> | |
<input type="text" name="name" id="name"> | |
<input type="submit" value="Greet Me"> | |
</form> | |
</body> | |
</html> | |
""".format(a=a, b=b, add=a+b, sub=a-b, mul=a*b, div=a/b) | |
# This route is where the above form will send the data. Note that the | |
# path and method match the <form> and @app.route() | |
@app.route('/form', methods=["POST"]) | |
def formSubmit(): | |
print(request.form) | |
name = request.form["name"] | |
print(name) | |
return """ | |
<html> | |
<head> | |
<title>Hello {name}</title> | |
</head> | |
<body> | |
<h1>Hello {name}</h1> | |
<p>Your name is</p> | |
<pre>{name}</pre> | |
<hr /> | |
<a href="/about">Go Back</a> | |
</body> | |
</html> | |
""".format(name=name) | |
app.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment