Advanced Import

The main reading said that from pkg import * is generally considered a bad idea. It is.

Nevertheless, I used that technique a few times to break up my Flask module into different files while preserving the simplicity of using url_for to specify the name of a handler, and @app.route() to add a route/handler, etc. You may find it useful in a multi-person group.

You should defer this reading until you know a bit about flask, say up until the lecture called flask3.

Specifically, I used it in the all_examples.py file in your flask3 folder. That file has some lines like:

from example_template_inheritance import *
from example_request_method import *
from example_form_processing_one_route import *
from example_post_redirect_get import *
from example_flashing import *

That imports functions in those files (more generally, the names in those modules) into the current program, so things in this file can refer to them easily.

Just taking one of those example modules, namely example_form_processing_one_route.py. Here is that entire file:

from flask import (Flask, url_for, render_template, request)
import math
from all_examples import app

@app.route('/ln/', methods=['GET','POST'])
def ln():
    if request.method == 'GET':
        return render_template('ln.html')
    else:
        x = request.form.get('x')
        try:
            y = math.log(float(x))
            return render_template(
                'ln.html',x=x,y=y)
        except:
             return render_template(
                 'ln.html',
                 msg=('Error computing ln of {x}'
                      .format(x=x)))

Notice (line 3) how it imports the app name from all_examples so that it gets the very same object to add the route to.

This technique allows us to chop a long app.py file into pieces. That can be convenient when working with several people in a team, so each person can have their own piece of app.py to work on. Imagine Harry, Ron and Hermione collaborating on a web app and the main app.py looks like:

from flask import (render_template ...)
import cs304dbi as dbi

app = Flask(__name__)

from harry_app import *
from ron_app import *
from hermione_app import *

Each of those files can start with:

from app import *

...

and then each of our team has their own file to work on.