Creating Web Applications with Flask: A Python Web Framework

flask n.w
1 / 11
Embed
Share

"Learn how to build web applications using Flask, a popular web framework for Python that simplifies web development by providing a collection of packages to handle low-level details like protocols and sockets. Explore routing, templates, and more in this Flask tutorial."

  • Flask Tutorial
  • Python Web Development
  • Web Frameworks
  • Flask Routing
  • Templates

Uploaded on | 1 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Flask Web Frameworks for Python jyheo@hansung.ac.kr

  2. Web Frameworks for Python A Web framework is a collection of packages or modules to write Web applications or services without having to handle such low-level details as protocols, sockets or process/thread management. Popular frameworks (https://wiki.python.org/moin/WebFrameworks/) Django web2py Flask Bottle CherryPy Raspbian includes Flask, don t need to install it.

  3. Flask - Minimal Application pi@raspberrypi:~ $ cat hello.py from flask import Flask app = Flask(__name__) @app.route('/') def hello_world(): return 'Hello, World!' if __name__ == '__main__': app.run(host='0.0.0.0 ) pi@raspberrypi:~ $ python3 hello.py * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit) 192.168.0.129 - - [02/Nov/2017 10:52:38] "GET / HTTP/1.1" 200 - 192.168.0.129 - - [02/Nov/2017 10:52:38] "GET /favicon.ico HTTP/1.1" 404 -

  4. Route() Decorator route() decorator is used to bind a function to a URL from flask import Flask app = Flask(__name__) @app.route('/') def index(): return 'Index Page' @app.route('/hello') def hello(): return 'Hello World' if __name__ == '__main__': app.run(host='0.0.0.0')

  5. Variable to URL from flask import Flask app = Flask(__name__) @app.route('/user/<username>') def show_user_profile(username): # show the user profile for that user return 'User %s' % username @app.route('/post/<int:post_id>') def show_post(post_id): # show the post with the given id, the id is an integer return 'Post %d' % post_id if __name__ == '__main__': app.run(host='0.0.0.0')

  6. Templates from flask import Flask from flask import render_template app = Flask(__name__) @app.route('/greeting') @app.route('/greeting/<name>') def hello(name=None): return render_template('greeting.html', name=name) if __name__ == '__main__': app.run(host='0.0.0.0')

  7. Templates render_template('greeting.html', name=name) greenting.html must be stored in templates/ $ cat templates/greeting.html <!doctype html> <title>Hello from Flask</title> {% if name %} <h1>Hello {{ name }}!</h1> {% else %} <h1>Hello World!</h1> {% endif %}

  8. Form & Request Object from flask import Flask, request @app.route('/formtest', methods=['POST', 'GET']) def form_test(): if request.method == 'POST': return 'Username: %s' % (request.form['username']) else: return ''' <form action="/formtest" method="post"> Name: <input name="username" type="text" /> <br/> <input value="Send" type="submit" /> </form> '''

  9. Session https://gist.github.com/jyheo/b53dd1f868a4ef25a282 http://127.0.0.1:5000/logout

  10. Tutorial http://flask.pocoo.org/docs/0.12/tutorial/

  11. Exercise @app.route('/list') def list(): retstr = "" with open('list.txt') as f: for l in f.readlines(): retstr += l + '<br/>' return retstr Write an BBS web service based on these example code. Use templates! @app.route('/write', methods=['POST', 'GET']) def write_article(): if request.method == 'POST': article = request.form['article'] with open('list.txt', 'a') as f: f.write(article) return 'Write Successful' return 'Write Error' else: return ''' <form action="/write" method="post"> Article: <input name="article" type="text" /> <br/> <input value="Write" type="submit" /> </form> ''' Hint: for can be used in a template!

More Related Content