Flask

Flask References:



AWS Configuration:



Minimal:

Notes:

  1.  Import the class Flask from the package flask.
  2. Instantiate the class, using the function __name__
  3. Tell the app to respond to the default (no path) URL
  4. Start the function that will be called when the URL is requested in a browser
  5. Return a trivial text message as the HTML page
  6. Start the flask server, listening on port 8080, using the current IP address of the current machine

# File: minFlask.py
from flask import Flask
app = Flask (__name__)
@app.route ("/")
def reply () :
  return "This is a simple web page"
app.run (host="0.0.0.0", port=8080)


Flask Directory Structure


Longer example:

# w6_02.py
sp = "/index.html"
si = """

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
Index.html, Links:<br>
<ul>
<li><a href="musings.html">Musings</a></li>
<li><a href="stuff.html">Stuff</a></li>
<li><a href="fun.html">Fun</a><br>
</li>
</ul>
</body>
</html>
"""
s1 = """
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <meta content="text/html; charset=windows-1252"
      http-equiv="Content-Type">
    <meta content="Nicholas Duchon" name="author">
    <title>Musing A - Java Stuff</title>
    <link href="highlighting.css" rel="stylesheet" type="text/css">
  </head>
  <body>
  <h1>Test Flask 03: Musings</h1>
    <table cellspacing="0" cellpadding="10" border="1">
      <tbody>
        <tr>
          <td valign="top" bgcolor="#ccffff" align="center"><b><font
                size="+1"> Java Stuff - <a href="/duchon/index.html">Home</a></font></b><b><br>
            </b> </td>
          <td valign="top" bgcolor="#ccffff" align="center"><b><font
                size="+1"> General</font></b><b><br>
            </b> </td>
          <td valign="top" bgcolor="#ccffff" align="center"><b><font
                size="+1"> Data Structures</font></b><b><br>
            </b> </td>
        </tr>
        <tr>
          <td valign="top">
            <ul type="disc">
              <li><a href="variables.html">Variables</a> - C </li>
              <li><a href="initializing.html">Initializing_Variables</a>
                - C </li>
              <li><a href="../vars/VariablesA.html">Variables_and_methods</a>
                - P<a href="../vars/VariablesA.html"><br>
                </a></li>
              <li><a href="methods.html">Methods</a> - C </li>
              <li><a href="http://sandsduchon.org/duchon/Musings/a/toString.html">toString</a>
                - W</li>
              <li><a href="http://sandsduchon.org/cm141/JavaSupplementaryMaterials/JavaSupplementaryMaterials.html">Static</a>
                -&nbsp; </li>
            </ul>
            <ul type="circle">
              <li><a href="exceptions.html">Exceptions</a> - W </li>
              <li><a href="reading.html">Reading a Text File - W</a><br>
              </li>
              <li><a href="scanner.html">Scanner_and_JFileChooser</a></li>
              <ul>
                <li>+ Mac issues - C </li>
              </ul>
              <li><a href="formatting.html">Formatting_with_printf</a> -
                C </li>
              <li><a href="printing.html">Printing_an_array</a> - C </li>
              <li><a
                  href="http://sandsduchon.org/duchon/Musings/a/varargs.html">Varargs</a></li>
              <li><a
                  href="http://sandsduchon.org/duchon/videos/arrayOfDice.html">Array
                  of Dice</a> (3D arrays) - V</li>
              <li><a href="interactive.html">Java_Interactive_I-O<br>
                  while(menu()) looping<br>
                  tokenizing<br>
                </a></li>
            </ul>
            <ul type="square">
              <li><a href="../../cs130/classes/ClassHierarchies.html">Class_Hierarchy</a>
                - W </li>
              <li><a
                  href="http://sandsduchon.org/duchon/Musings/a/constructors.html">Constructors
                  with Initializers</a></li>
              <li><a href="scanner2.html">Scanner Constructors</a></li>
              <ul>
                <li>CA, CB, ..., CG<br>
                </li>
              </ul>
              <li><a href="polymorphism.html">Polymorphism Method
                  Resolution</a></li>
            </ul>
            <ul>
              <li><a href="dates.html">Parsing_dates</a> - C </li>
              <li><a href="generics.html">Generic_Classes</a> - CW </li>
              <li><a href="commandLine.html">Command_Line_Arguments</a>
                - C </li>
              <li><a href="this.html">this_-_a_compelling_example</a> -
                C </li>
            </ul>
            <ul type="circle">
              <li> <a href="dump.html">Dumping a stack</a></li>
              <li> <a
                  href="http://sandsduchon.org/duchon/cs335/threads/index.html">Threads
                  Examples + Executor Pools</a></li>
              <li> <a href="threads1.html">Threads</a></li>
              <li><a
                  href="http://sandsduchon.org/duchon/cs335/threads/locks.html">Locks
                  Notes</a></li>
              <li> <a
                  href="http://sandsduchon.org/duchon/jdk/lambda.html">JDK
                  8 Lambda, :: operator<br>
                </a></li>
            </ul>
          </td>
          <td valign="top">
            <h4>Projects:</h4>
            <ul>
              <li><a href="http://sandsduchon.org/duchon/cs130/case2/Sample_Project_Overview.html">A
                  simple project</a></li>
              <li><a href="http://sandsduchon.org/duchon/interfaces/myif/index.html">A
                  GUI project with sorting</a></li>
              <li><a href="http://sandsduchon.org/duchon/cs335/cave/strategy.html">A
                  project with classes</a><br>
              </li>
            </ul>
            <h4>GUI\'s:<a href="#addActionListener_%28this%29"><br>
              </a></h4>
            <ul>
            </ul>
            <a href="http://sandsduchon.org/duchon/Musings/a/fxNotes.html">JavaFX
              Notes</a>
            <ul>
              <li><a href="http://sandsduchon.org/duchon/Musings/a/hello.html">GUI
                  Hello</a></li>
              <li><a href="http://sandsduchon.org/duchon/Musings/a/actions.html">Action</a>
                listeners - W <br>
              </li>
              <li><a href="http://sandsduchon.org/duchon/cs335/cave/strategy.html#14:_Java_8_Listener_Syntax">ActionListener</a>
                Java 8 lambda - C<a href="http://sandsduchon.org/duchon/cs335/cave/strategy.html#14:_Java_8_Listener_Syntax"><br>
                </a></li>
              <li><a href="anonymousListeners.html">Anonymous_Listeners</a>
                - C&nbsp;</li>
              <li><a href="buttons.html">Buttons_on_a_JPanel_with_Listeners
                  - C&nbsp;</a></li>
              <li><a href="basicJApplet.html">Basic JApplet Starting
                  Button</a> - C <br>
              </li>
              <li><a href="basicApplet.html">Basic Applet and
                  Application</a></li>
              <li><a href="textarea.html">JTextArea with scrolling</a></li>
              <li><a href="macButton.html">JButton color on a Mac<br>
                  Looks and Feels</a><br>
              </li>
              <li><a href="drawing.html">Drawing panel with scrolling</a></li>
              <li><a href="http://sandsduchon.org/duchon/cs335/Resize.html">Resize
                  Demonstration</a></li>
              <li><a href="http://sandsduchon.org/duchon/Musings/a/animation.html">Simple
                  Java Animation</a></li>
              <li><a href="http://sandsduchon.org/duchon/Musings/a/manyWindows.html">Multiple
                  Windows, FrameND<br>
                </a></li>
              <li><a href="http://sandsduchon.org/duchon/2014/sp/cs335/discovery.html">Layout
                  Managers<br>
                </a>including JSplitPane
                <a href="http://sandsduchon.org/duchon/2014/sp/cs335/discovery.html"><br>
                </a></li>
              <li><a href="http://sandsduchon.org/duchon/Musings/a/table.html">JTable
                  examples</a></li>
              <li><a href="http://sandsduchon.org/duchon/Musings/a/tree.html">JTree
                  examples</a></li>
              <li><a href="http://sandsduchon.org/duchon/images/Liang16/">Liang
                  Ch 16 17 images</a></li>
            </ul>
            <h4>Recursion:</h4>
            <ul>
              <li><a href="http://sandsduchon.org/duchon/Musings/a/recursion.html">Recursion</a>
              - C <br>
                JOptionPane for input<br>
              </li>
              <li><a href="http://sandsduchon.org/duchon/cm102/PascalsTriangle/PascalsTriangle.html">Recursion_and_Pascal\'s_Triangle</a>
                - W&nbsp;</li>
              <li><a href="http://sandsduchon.org/duchon/Musings/a/recursionBlobs.html">Recursion_and_Blobs</a>,
                2D - C&nbsp;</li>
              <li><a href="http://sandsduchon.org/duchon/Musings/a/recursionKoch.html">Recursion_-_Koch_snowflake</a>
                - C </li>
              <li><a href="http://sandsduchon.org/duchon/Musings/a/pnexample.html">Prefix
                  Example</a></li>
            </ul>
          </td>
          <td valign="top">
            <ul>
              <li> <a href="summary.html">Summary of Data Structures</a></li>
              <li><a href="../DataFiles.zip">Data Files - zip (2MB)</a></li>
              <li> <a href="survey.html">Data_structures_Survey</a> - W
              </li>
              <li> <a href="slowSorts.html">The_slow_sorts</a></li>
              <li> <a href="bigO.html">Big-O</a></li>
              <li> <a href="comparing.html">Comparing_using_Comparators</a>
                - C </li>
              <li><a href="../../cs335/cave/cave004.html">Sorting with
                  enum methods (Cave004)</a></li>
              <ul>
                <li>Lambda expressions</li>
                <li>JComboBox</li>
                <li>enums, bifunctions<br>
                </li>
              </ul>
              <li> <a href="listDemo.html">ListDemo_from_Liang</a> - V
              </li>
              <li> <a href="cloning.html">Cloning</a></li>
              <li> <a href="search.html">Binary_Search_Tree_Simplified</a>
                - C </li>
              <li> <a href="traversals.html">Tree_Traversals</a> - W </li>
              <li> <a href="../../cs230/avlAlgorithm.html">AVL
                  Algorithm</a> - W </li>
              <li><a href="http://sandsduchon.org/duchon/2014/sp/cs335/MusingsAll.html#JTree_and">JTree
                  more comments</a></li>
              <li><a href="hashmaps.html">HashMap example</a><br>
              </li>
            </ul>
            <h4>Related Conversations</h4>
            <ul>
              <li><a href="http://sandsduchon.org/duchon/cm242/review.html">CMIS
                  141 - Review</a></li>
              <li><a href="http://sandsduchon.org/duchon/2014/sp/cs335/MusingsAll.html">CMSC
                  335 - Musings<br>
                </a></li>
              <li><a
                  href="http://sandsduchon.org/duchon/cs335/index.html">CMSC
                  335 - Information</a></li>
              <li><a href="http://sandsduchon.org/duchon/math/index.html">Math
                  Stuff</a></li>
            </ul>
            <h4>Interfaces:</h4>
            <ul>
              <li><a href="http://sandsduchon.org/duchon/Musings/a/interfaces1.html">Interfaces
                  Survey</a> - W <br>
              </li>
              <li><a href="http://sandsduchon.org/duchon/interfaces/myif/index.html">MyIF
                  Development</a> - WC<a
                  href="http://sandsduchon.org/duchon/interfaces/myif/index.html"><br>
                </a>Comparable, Comparator, Font<a
                  href="http://sandsduchon.org/duchon/interfaces/myif/index.html"><br>
                </a>Lambda, bifunctions<a
                  href="http://sandsduchon.org/duchon/interfaces/myif/index.html"><br>
                </a></li>
              <li> <a href="http://sandsduchon.org/duchon/interfaces/InterfacesPart1.pdf">Tutorial
                  Part 1</a> - P<br>
                event listeners<br>
              </li>
              <li> <a href="http://sandsduchon.org/duchon/interfaces/InterfacesPart2.pdf">Tutorial
                  Part 2</a> - P<br>
                sorting, collections, comparators<br>
              </li>
            </ul>
          </td>
        </tr>
        <tr>
          <td valign="top" bgcolor="#ffff99" align="center">
            <h3>Algorithm Stuff</h3>
          </td>
          <td valign="top" bgcolor="#ffff99" align="center">
            <h3>Development notes</h3>
          </td>
          <td valign="top" bgcolor="#ffff99" align="center">
            <h3>Examples</h3>
          </td>
        </tr>
        <tr>
          <td valign="top">
            <ul>
              <li><a href="../../cm310/BaseTranslations.html">Converting_Bases</a>
                - W </li>
              <li> <a href="random.html">Random_Numbers</a></li>
              <li> <a href="randomDemo.html">Random Demonstration</a></li>
              <li> <a href="../../exTrees/index.html">Expression_Trees</a><br>
                infix, postfix - CWA</li>
              <li> <a href="http://sandsduchon.org/duchon/math/permutations.html">Permutations</a>
                - WC<br>
                recursion, ArrayList&lt;Path&gt;<br>
              </li>
            </ul>
          </td>
          <td valign="top"><a href="jgrasp.html">jGrasp visualization</a><br>
            <a href="../../unix/notes.html">UNIX commands</a> - W<br>
            <a href="../../interfaces/myif/index.html">JApplet_to_Interface_-_MyIF</a>
            - WC<br>
            <a href="../../2017/sp/cm242/cdfp4code.html">jar accessing
              resources</a><br>
            <a href="../../cs130/javaPlaces.html">On_line_resources</a>
            - W<br>
            <a href="uml.html">UML Tools</a><br>
            <a href="jconsole.html">jconsole</a><br>
            <a href="../../cs335/CaveNetBeansDebugging.swf">Debugging
              with NetBeans</a><br>
            <a
              href="http://sandsduchon.org/duchon/Musings/a/warnings.html">IDE
              Warnings</a><br>
          </td>
          <td valign="top"><a href="../../cs230/p2.3/index.html">Change_Calculator</a>
            - CA <br>
            <a href="paths.html">Counting_Paths</a> - V <br>
            <a href="autoincrement.html">Nasty_autoincrement</a> - C<br>
            <a href="../../cs335/movingLine.html">Moving on a Line</a><br>
            <a href="../../cs335/trains/mobile.html">Trains</a><br>
            <a href="serialization.html">Serialization Example</a> - C<br>
            <a
              href="http://sandsduchon.org/duchon/Musings/a/shapes.html">Shapes
              classes in Java</a> - CW<br>
            <a href="http://sandsduchon.org/duchon/Musings/manPage.html">Man
              Page Example</a> - CW (initializer &amp; GUI)<br>
            <a href="http://sandsduchon.org/duchon/cs335/calcRND.html">CalculatorRaleyND
              - CW</a><br>
            <a href="/duchon/cs335/applet/BasicScrollingDrawingApplet.html">KnightsTourApplet.html</a><br>
            <a href="http://sandsduchon.org/duchon/math/trees.html">Expression
              Trees</a><br>
            <a href="http://sandsduchon.org/duchon/Musings/a/String_vs_Builder.html">String_vs_StringBuilder</a><br>
            <a href="balls.html">Bouncing Balls</a> - consumer function<br>
          </td>
        </tr>
      </tbody>
    </table>
    <p>Notes:</p>
    <ul>
      <li>A - Applet </li>
      <li>C - code example</li>
      <li>V - video </li>
      <li>W - additional web pages <br>
      </li>
      <li>P - pdf file<br>
      </li>
    </ul>
    <hr size="2" width="100%">
    <h2>End.<br>
    </h2>
  </body>
</html>
"""

s2 = """
<html>
<head>

<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<h1>This is another way to do stuff:</h1>
<ul>
<li>lkjsdf</li>
<li>sldfkj</li>
<ul>
<li>lksdjf</li>
<ul>
<li>lkjsdf</li>
</ul>
<li>sldfkjoeiru</li>
<ul>
<li>lksdjf</li>
<li>lkjsdfl</li>
</ul>
<li>woieuroi</li>
</ul>
<li>348s9032</li>
</ul>
<ol>
<li>owieur</li>
<ol>
<li>oriuowiur</li>
<ol>
<li>ityweur</li>
<li>weoiroi</li>
<li>oweoir</li>
</ol>
<li>oeiru923</li>
<li>324</li>
<li>23478924</li>
<li>23984774</li>
</ol>
<li>sdjflks</li>
<li>029348</li>
</ol>
<hr size="2" width="100%"><br>
<table cellspacing="2" cellpadding="2" border="1">
<tbody>
<tr>
<td valign="top">lsdkjdflkjdsf<br>
</td>
<td valign="top">ldskoiweuroiu<br>
</td>
</tr>
<tr>
<td valign="top">dsdoviuoiwer<br>
</td>
<td valign="top">iuj,cnvjdsfkjhs<br>
</td>
</tr>
</tbody>
</table>
<p><br>
</p>
</body>
</html>
"""

s3 = """
<html>
<head>

<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body bgcolor="#993399">
<h1>
<font color="#ffcc33">
Is this fun or what?
</font>
</h1>
</body>
</html>
"""

from flask import Flask

app = Flask(__name__)

@app.route(sp)
@app.route('/')

def helloIndex():
   return si

@app.route('/musings.html')

def musings_rt():
   return s1

@app.route('/stuff.html')

def stuff_rt():
   return s2

@app.route('/fun.html')

def fun_rt():
   return s3

# standard run host server command:  
app.run(host='0.0.0.0', port= 8080)



Output:

 * Serving Flask app "w6_02" (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://0.0.0.0:8080/ (Press CTRL+C to quit)
10.0.0.66 - - [25/Nov/2019 18:57:33] "GET /index.html HTTP/1.1" 200 -
10.0.0.66 - - [25/Nov/2019 18:57:34] "GET /musings.html HTTP/1.1" 200 -
10.0.0.66 - - [25/Nov/2019 18:57:34] "GET /highlighting.css HTTP/1.1" 404 -
10.0.0.66 - - [25/Nov/2019 18:57:37] "GET /stuff.html HTTP/1.1" 200 -
10.0.0.66 - - [25/Nov/2019 18:57:40] "GET /fun.html HTTP/1.1" 200 -
10.0.0.66 - - [25/Nov/2019 18:57:44] "GET /favicon.ico HTTP/1.1" 404 -
10.0.0.66 - - [25/Nov/2019 18:58:10] "GET / HTTP/1.1" 200 -
10.0.0.66 - - [25/Nov/2019 18:58:10] "GET /favicon.ico HTTP/1.1" 404 -
10.0.0.66 - - [25/Nov/2019 18:58:13] "GET /musings.html HTTP/1.1" 200 -
10.0.0.66 - - [25/Nov/2019 18:58:13] "GET /highlighting.css HTTP/1.1" 404 -
10.0.0.66 - - [25/Nov/2019 18:58:17] "GET / HTTP/1.1" 200 -



GET/POST Example:

# w7v01.py

si = """
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
   <head>
     
      <meta http-equiv="content-type" content="text/html; charset=UTF-8">
      <title>Login to My Site</title>
      <style>
         label {
            display: block;
            float: left;
            width: 150px;
            text-align: right;
            font-weight: bold;
            margin-right: 10px;
         }
        
         input.submit {
            margin-left: 160px;
         }
      </style>
     
   </head>
      <body>
         Index.html, Links:<br>
         <ul>
            <li>Musings</li>
            <li>Stuff</li>
            <li>Fun<br>
            </li>
         </ul>
         <form action=login method=post>
            <label>User Name:</label>
            <input type=text name=un>
            <br>
            <label>Password:</label>
            <input type=password name=pwd>
            <br>
         <input type="submit" class=submit value="log in">
         </form>
      </body>
</html>
"""

sinput = """
      <style>
         label {
            display: block;
            float: left;
            width: 150px;
            text-align: right;
            font-weight: bold;
            margin-right: 10px;
         }
        
         input.submit {
            margin-left: 160px;
         }
      </style>
"""

def s01 (st):
   return  """
   <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
   <html>
      <head>
        
         <meta http-equiv="content-type" content="text/html; charset=UTF-8">
         <title>""" + st + """</title>"""

        
s02 = """</head><body>"""

s99 = """</body></html>"""


from flask import Flask

app = Flask(__name__)

@app.route("/index.html", methods=['GET', 'POST'])
@app.route('/')

def helloIndex():
   return si

@app.route('/a/<var>')

# http://10.0.0.66:8080/a/other
def varval(var):
   s2  = str(s01 ("kindness"))
   s2 += "Post value >"
   s2 += str(var)
   s2 += "<"
   s2 += s99
   return s2

from flask import request

# do both get & post to get POST values
# default is just get values
@app.route('/login', methods=['GET', 'POST'])

# http://10.0.0.66:8080/a/other
def logfunc():
   v = "none: "
   w1 = "nonew"
   if request.method == 'POST':
      v = "posting: "
      print ("Posting")
      w1 = request.form
   if request.method == 'GET':
      v = "getting: "
      print ("Getting")
      w1 = request.args
   print (v + str(w1))
   s2  = str(s01 ("Stranger 01"))
   s2 += sinput
   s2 += s02
   s2 += "Get values >"
   s2 += v
   s2 += "<"
   s2 += "<P>"
   s2 += "un >"
   s2 += str(w1.get("un"))
   s2 += "<"
   s2 += "<P>"
   s2 += "pwd >"
   s2 += str(w1.get("pwd"))
   s2 += "<"
   s2 += """
         <form action="index.html" method=post>
         This is a form.
         <P>
         <input type="submit" value="return">
         </form>
   """
   s2 += s99

   return s2

@app.route('/b', methods=['GET', 'POST'])

# http://10.0.0.66:8080/b?xname=324
def postget():
   v = "none"
   if request.method == 'POST':
      v = "posting"
   if request.method == 'GET':
      v = "getting"
   w = request.args.get ("xname", "")
   print (request.args)
   s2  = str(s01 ("kindness"))
   s2 += "Post value >"
   s2 += v
   s2 += "<"
   s2 += "<P>"
   s2 += "xname >"
   s2 += w
   s2 += "<"
   s2 += s99
   return s2

# standard run host server command:  
app.run(host='0.0.0.0', port= 8080)


flaskr

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

Steps:

  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:
    $ pwd
    /home/ec2-user/environment/flaskr_tutorial
    $ dir -F
    flaskr/  instance/  LICENSE  MANIFEST.in  README.md  setup.cfg  setup.py
  3. Set the required environment variables:
    $ export FLASK_APP=flaskr; export FLASK_ENV=development
  4. Initialize the database:
    $ flask init-db
  5. Ready to run flaskr
    1. with no parameters, default is local host, ip=127.0.0.1, port=5000
      flask run
    2. letting the "universe" know about your server, port still 5000
      flask run --host=0.0.0.0

(end for now)



(end)