MCS 275 Spring 2021
Emily Dumas
Course bulletins:
python3 -m pip install Flask
in preparation for using it in upcoming assignments.First, let's check in on front page mockups for our two apps (chat and vote) with updated CSS.
Reminder: You can always get the code from the sample code repository.
So far, I've been opening files in the web browser, using URLs with the file
protocol.
There's no network communication here. The browser just opens the file using the OS interface.
To make an actual web site or application, we need an HTTP server.
python3 -m http.server
Opens a web server that serves files in the current directory and its subdirectories.
Visit http://localhost:8000/
in a browser (or substitute other port number shown in startup message) to see index.html
.
Firewall rules typically prevent incoming connections from the internet (and maybe the local network too). That's good! Or
python3 -m http.server --bind 127.0.0.1
will make sure it only listens for connections from the same machine.
Most HTTP servers that deliver resources from a filesystem will look for a file called index.html
and send it in response to a request that ends in a /
.
(i.e. if no filename is given, index.html
is used.)
e.g. GET /teaching/2020/fall/mcs260/
is sent to dumas.io
when you load the home page of my Fall 2020 MCS 260 course.
More detailed look at an HTTP GET request: MCS 260 Fall 2020 Lecture 33.
The answer to any HTTP request includes a numeric code indicating success or error.
There are lots of codes; the first digit is often all you need to know:
Flask is a Python web framework. It makes it easy to write Python programs that respond to HTTP requests (e.g. web applications, APIs).
Competitors include:
from flask import Flask
app = Flask(__name__)
@app.route("/positivity/")
def name_of_function_does_not_matter():
return """<!doctype html>
<html>
<body>
You can do it!
</body>
</html>
"""
app.run()