Code Monkey home page Code Monkey logo

flask-lecture's Introduction

Introduction to Flask

Demo project -> https://chaosarium.pythonanywhere.com
Code on GitHub -> https://github.com/chaosarium/flask-lecture
Docs -> https://chaosarium.gitbook.io/113-flask

Flask is a Python web framework, i.e. something that lets you build web apps!

One cool thing you can then do is to make different machines talk to each other — you can even have things written in different languages talk to each

But first, how does the web work? See HTTP if not already familiar.

Files in the repo/zip file

  • basic contains demo of Flask app setup and basic routes
  • topiclist contains the demo project
  • docs is the documentation source
  • starter_code is where you can start implementing the demo project (or something else)

Setting up a Flask app

Create a file app.py (usually they call it that and it works)

Of course you import a bunch of things

from flask import Flask
from flask import request # now we can also use different HTTP methods
from flask import render_template # for rendering html templates
...

This line creates a flask app

app = Flask(__name__)

And this lets you run the app when you run python app.py in terminal

if __name__ == '__main__':
    # here, 127.0.0.1 is the IP address for localhost, and port can be though of the channel at this address?
    app.run(host='127.0.0.1', port = 5000)
    # If you want your app to be available publically, you change the host to 0.0.0.0. Then people in your local network should be able to access your app via your computer's IP
    # Note that your computer probably doesn't have a public IP, so someone in, California, for example, won't be able to access your app (unless they go on CMU VPN(?))
    # If you want your app to be made public everywhere, you need a public IP.

But wait, we just created an app that doesn't "listen" to anything. We need to define functions so that it handles http requests like the one we saw earlier.

Here's the code for a function that listens at / and responds by sending hello world.

@app.route("/")
def root_route():
    return "Hello, World!" 

Flask uses some sort of function decorator. We already said app = Flask(__name__), so app.route("...") is creating a route for the app. And "/" just means root URL. Flasks makes it so that return sends our response. In this case we're just sending text.

Putting it together, we have:

from flask import Flask
from flask import request
from flask import render_template

app = Flask(__name__)

@app.route("/")
def root_route():
    return "Hello, World!" 

if __name__ == '__main__':
    app.run(host='127.0.0.1', port = 5000)

More routes

We can do more than just sending back hello world — we can get data from the request, do something with it, and send back something fancy!

We can have non-root url

@app.route('/projects')
def projects():
    return 'The project page'

We can capture path pattern in url

@app.route('/user/<username>')
def user_profile(username):
    print(username)
    return f'hmm, see console'

We can specify the type. Also we are returning some html here @app.route('/fact/int:n')

def fact_page(n):
    return f'<p style="overflow-wrap: anywhere;">{n}! = {fact(n)}</p>'

We can do something different depending on what type of request we got

@app.route('/getorpost', methods=["GET", "POST"])
def getorpost():
    if request.method == 'POST':
        return "method was POST"
    else:
        return "method was GET"

We can render html file. This example captures name from the request url and puts it in a sayhello template, which flask will look for in the ./templates directory.

@app.route('/sayhello/<name>')
def sayhello(name):
    return render_template("sayhello.html", name = name)

We can return json too

@app.route("/api")
def api():
    return {
        "foo": "fooo",
        "bar": "barr",
        "quote": "hello world",
    }

Running a Flask app

There is a way to start a server with the flask command. You also have the option of enabling debug mode.

flask run

or

flask --debug run

or you can make the app run when you run the python file by writing:

if __name__ == '__main__':
    app.run(host='127.0.0.1', port = 5000) # this runs it on local network
    # app.run(host='0.0.0.0', port = 5000) # this makes it public

Example project — a 15113 topic list

Demo link: https://chaosarium.pythonanywhere.com

What we need:

  • Some database to store data. See database
  • A home page that displays data (GET)
  • Places to update data using POST
    • /add topic
    • /promote topic
    • /reject topic
  • Some interface to send POST requests
  • UI design (maybe) (implemented on the stylish-topiclist branch)

pic

Deployment

There are many options for deploying your Flask app. We'll use PythonAnywhere because of its simplicity.

See deployment

flask-lecture's People

Contributors

chaosarium avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.