Flaskr, adding to

Flask References:


Setting up the environment for the tutorial is rather complex, but here are some notes.


  1. Download or copy/paste the new python source code for flaskr from github
    1. flask/examples/tutorial at master · pallets/flask · GitHub
  2. Get in the right directory - the parent of the flaskr directory:
    $ dir -F
    flaskr/  instance/  LICENSE  MANIFEST.in  README.md  setup.cfg  setup.py
  3. Set the required environment variables:
    1. Linux/Mac:
      $ export FLASK_APP=flaskr; export FLASK_ENV=development
    2. Windows:
      > C:\path\to\app>set FLASK_APP=hello.py
      > C:\path\to\app>set FLASK_ENV=development
  4. Initialize the database:
    $ flask init-db
  5. Ready to run flaskr
    1. with no parameters, default is local host, ip=, port=5000
      flask run
    2. letting the "universe" know about your server, port still 5000
      flask run --host=

Steps for lab 7 (Mac)

  1. Copy image files to static directory.
  2. Create or copy a .py file for the additions to the project, say nick.py (or lab7.py?)
  3. Create a directory under templates: templates.nick or templates.lab7, say.
  4. Copy or create some html files into templates.nick (or so) - pagea.html, pageb.html, table.html in my case.
  5. __init__.py:
    from flaskr import auth, blog, nick
  6. nick.py
    1. from flask import Blueprint
    2. from flask import render_template
    3. from flask import redirect
    4. bp = Blueprint("nick", __name__)
    5. import time
      def curtime () :
        return time.asctime(time.localtime(time.time()))
    6. @bp.route ("/a")
      def replyA () :
         return render_template ('nick/pagea.html', title="page a", time=curtime())
    7. repeat 6 for other pages, in my case: /b and /table
  7. base.html, adding - YOUR links should link to YOUR pages, of course:
       Links to our pages:
          <li><a href='/'>Home</a>
          <li><a href='/a'>page a</a>
          <li><a href='/b'>page b</a>
          <li><a href='/table'>table</a>
      {% block content %}{% endblock %}
       {% if time %}
          <hr>Time: {{ time }}
       {% endif %}
  8. Run flask and check to find files using:
    1. flask run
  9. If you want to use the login system already present in flaskr, add the following:
    1. from flaskr.auth import login_required
      @bp.route ("/table")

Sep 3, 2020 - Nick Duchon