flask pyth thon on web eb fram amewor orks ks
play

Flask Pyth thon on web eb fram amewor orks ks Django Roughly - PowerPoint PPT Presentation

Flask Pyth thon on web eb fram amewor orks ks Django Roughly follows MVC pattern Steeper learning curve. Flask Initially an April Fools joke Micro -framework: minimal approach. Smaller learning curve


  1. Flask

  2. Pyth thon on web eb fram amewor orks ks  Django  Roughly follows MVC pattern  Steeper learning curve.  Flask  Initially an April Fools joke  “Micro” -framework: minimal approach.  Smaller learning curve  http://flask.pocoo.org/docs/0.12/quickstart/#a-minimal-application  Scaffolded video tutorial  https://www.youtube.com/watch?v=iSrZ6r7hwdM&list=PL0DA14EB3618A350 7 Portland State University CS 430P/530 Internet, Web & Cloud Systems

  3. Fl Flas ask k direct rector ory y st struct ucture ure  ./ app.py  Entry-point for program static/  Directory holding static content (e.g. images, regular HTML) templates/  Directory holding Jinja2 HTML templates Portland State University CS 430P/530 Internet, Web & Cloud Systems

  4. Hello ello world ld  app.py  Create app as a Flask object  Use route() method of Flask with parameter '/' to wrap a function that will return a page when the URL is accessed  When called directly by python , use the run() method of Flask to launch the web app on all IP addresses of the host using port 8888  Not run when integrated with apache2/nginx, wsgi Portland State University CS 430P/530 Internet, Web & Cloud Systems

  5. % virtualenv – p python3 env % source env/bin/activate % pip install flask % python app.py * Serving Flask app "foo" (lazy loading) * Environment: production WARNING: Do not use the development server in a production environment. Use a production WSGI server instead. * Debug mode: off * Running on http://0.0.0.0:8888/ (Press CTRL+C to quit) Portland State University CS 430P/530 Internet, Web & Cloud Systems

  6. Gu Gues estbo tbook ok app pplica licatio tion n (v1) . ├── Model.py ├── model_pylist.py  https://bitbucket.org/wuchangfeng/cs430-src ├── app.py ├── requirements.txt  WebDev_Guestbook_v1_pylist ├── templates  Simple MVC app in Python/Flask │ ├── layout.html │ └── index.html  Models └── static  Base class Model to define interface in Model.py └── style.css  Important to implement additional models  Python lists model in model_pylist.py  Stub for SQLite model in model_sqlite3.py  Controller and entrypoint in app.py  View in Jinja2 templates  Base HTML in templates/layout.html  Derived single page templates/index.html  Python package requirements for pip  requirements.txt Portland State University CS 430P/530 Internet, Web & Cloud Systems

  7. Model del (base se) ) in Model.py . ├── Model.py ├── model_pylist.py  Base class for model ├── app.py ├── requirements.txt  Defines interface derived models must adhere to ├── templates  Two functions │ ├── layout.html │ └── index.html  select() to return list containing all entries └── static  insert() to insert new entry └── style.css class Model (): def select( self ): """ Gets all entries from the database :return: List of lists containing all rows of database """ pass def insert( self , name, email, message): """ Inserts entry into database :param name: String :param email: String :param message: String :return: True :raises: Database errors on connection and insertion """ pass Portland State University CS 430P/530 Internet, Web & Cloud Systems

  8. model_pylist.py . ├── Model.py ├── model_pylist.py  Derived class that implements Model using ├── app.py ├── requirements.txt Python lists ├── templates  Does not persist when server restarts │ ├── layout.html │ └── index.html  Not appropriate for multi-threaded servers or cloud └── static deployments └── style.css class model (Model): """ Initializes null list """ def __init__( self ): self .guestentries = [] """ Return current list """ def select( self ): return self .guestentries """ Append list containing name, e-mail, date, and message to guestentries list """ def insert( self , name, email, message): params = [name, email, date.today(), message] self .guestentries.append(params) return True Portland State University CS 430P/530 Internet, Web & Cloud Systems

  9. Contr troller oller in app.py . ├── Model.py ├── model_pylist.py  Import functions from flask, set model ├── app.py  Create app and model ├── requirements.txt ├── templates  Register route to implement main page │ ├── layout.html  Define function to get all entries from model via select() │ └── index.html  Uses a list comprehension to create a list of dictionaries with all entries └── static  Pass dictionary to index.html Jinja2 template via └── style.css render_template to return rendered page from flask import Flask, redirect, request, url_for, render_template # from model_sqlite3 import model from model_pylist import model # Set model app = Flask(__name__) # Create Flask app model = model() @app .route('/') # Function decorator ==> app.route('/',index()) @app .route('/index.html') def index(): # Generate list of dicts containing entries entries = [dict(name=row[0], email=row[1], signed_on=row[2], message=row[3] ) for row in model.select()] return render_template('index.html', entries=entries) Portland State University CS 430P/530 Internet, Web & Cloud Systems

  10.  Register route to implement POST handling of . sign URL in HTML form submission (will see ├── Model.py ├── model_pylist.py this later) ├── app.py  Insert entry via model's insert() method ├── requirements.txt ├── templates  Use Flask's request.form to get form │ ├── layout.html submission values by name │ └── index.html └── static  Note: model class will insert signed_on date └── style.css  Redirect user back to main page  Note: sign route does not have GET method @app .route('/sign', methods=['POST']) def sign(): model.insert(request.form['name'], request.form['email'], request.form['message']) return redirect(url_for('index')) Portland State University CS 430P/530 Internet, Web & Cloud Systems

  11. View w via Ji Jinja nja2 2 tem empla plates es . ├── Model.py ├── model_pylist.py  Base page is like a base class ├── app.py ├── requirements.txt  Sub pages are like derived classes ├── templates  App base page layout.html │ ├── layout.html │ └── index.html  Contains empty content block └── static └── style.css <!doctype html> <html> <title> My Visitors </title> <link rel=stylesheet type=text/css href="{{ url_for('static', filename='style.css') }}" > <div class=page > {% block content %} {% endblock %} </div> ... ... </html> Portland State University CS 430P/530 Internet, Web & Cloud Systems

  12. .  Derived page index.html that fills in ├── Model.py content block ├── model_pylist.py ├── app.py  Sequence through entries list of dictionaries ├── requirements.txt containing guestbook data ├── templates │ ├── layout.html │ └── index.html {% extends "layout.html" %} └── static {% block content %} └── style.css < h2 >Guestbook</ h2 > < form action="{{ url_for('sign') }}" method=post> < p class="heading">Name: < input type=text name=name></ p > < p class="heading">Email: < input type=text name=email></ p > < p class="heading">Message: < textarea rows=5 cols=50 name=message></ textarea ></ p > < p >< input type=submit value=Sign></ p > </ form > < h2 >Entries</ h2 > {% for entry in entries %} < p class=entry> {{ entry.name }} &lt; {{ entry.email }} &gt; < br > signed on {{ entry.signed_on }}< br > {{ entry.message }} </ p > {% endfor %} {% endblock %} Portland State University CS 430P/530 Internet, Web & Cloud Systems

  13. Gu Gues estbo tbook ok app pp v2 ├── app.py ├── gbmodel │ ├── __init__.py │ ├── Model.py │ ├── model_pylist.py │ └── model_sqlite3.py ├── index.py ├── sign.py └── templates ├── index.html └── sign.html  https://bitbucket.org/wuchangfeng/cs430-src  WebDev_Guestbook_v2_modules_mvp  Modularize code via Python packages  Create package for models named gbmodel  Directory name used as package name (e.g. import gbmodel within app.py )  __init__.py executed upon import  Organize into Model-View-Presenter pattern with Flask views  Index presenter for viewing guestbook ( index.py , index.html )  Sign presenter for inserting new entry ( sign.py , sign.html ) Portland State University CS 430P/530 Internet, Web & Cloud Systems

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend