`
liaofeng_xiao
  • 浏览: 127449 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

flask学习笔记

 
阅读更多
Micro means Flask aims to keep the core simple but extensible.
Flask won't make any decisions for you(database, template, form validation, authorization, uploading file, etc), everything is up to you. so flask can be anything you need and nothing you don't.

conventions:
static files and templates are stored in application's subdirectories within the application's Python source code tree, named static and templates, and this can be changed if you want to.

class Flask is designed to inherit, so you can customize your application behavior.

Thread-locals in Flask:

One of the design decisions in Flask was that simple tasks should be simple; they should not take a lot of code and yet they should not limit you. Because of that, Flask has few design choices that some people might find surprising or unorthodox. For ex- ample, Flask uses thread-local objects internally so that you don’t have to pass objects around from function to function within a request in order to stay threadsafe. This approach is convenient, but requires a valid request context for dependency injection or when attempting to reuse code which uses a value pegged to the request. The Flask project is honest about thread-locals, does not hide them, and calls out in the code and documentation where they are used.

Always build web application with CAUTION:
Flask and Jinja2 protect you from XSS

Installation:
Flask depends on Werkzeug and Jinja2. Werkzeug is a toolkit fo WSGI, the standard Python interface between web applications and variety of servers for both development and deployment.

virtualenv:
a really nice tool.

in virtualenv mode:
    pip install Flask
or system-wide installation:
    sudo pip install Flask

Quick start:

hello-word:

from flask import Flask                                                           

##__name__ is needed for Flask to know where is current module or package is,
##and where to find static and templates.                                                      
app = Flask(__name__)                                                             
@app.route("/")                                                                   
def hello_world():                                                                
    return "Hello world"                                                          
                                                                                  
if __name__ == "__main__":                                                        
    app.run()

now access 192.168.1.1:5000 you will get "Hello world". but the application is only accessible in your computer, use app.run(host=0.0.0.0) to tell the operation system to listen on all public IPs.

Debug Mode:
debug mode let Flask restart the web application automatically when detects source code changes.
two ways to implement:
1. app.debug = True
2. app.run(debug=True)

Routing: to build beautiful URLs.

@app.route("/")
@app.route("/projects/"): access /projects will to /projects/, projects looks like a directory
@app.route("/about"): access /about/ will 404, about behaves like a file
@app.route("/post/<post_id>/"): access post_id as dynamic argument
@app.route("/user/<int:user_id>/"): receive int user_id, and user_id as the function argument, int is converter
@app.route("/user/<path:url>"): url can be abc, or ab/c, path converter accepts slashes.
existing converters:
1. int:
2. float:
3. path: like the default converter but accepts slashes.

URL Building:
url_for方法能输出指定controller方法对应的url,为什么不在template里直接使用硬编码的url而要使用url_for方法呢?
1. url_for比硬编码的url更具有描述性(更好记)(可我没发现),而且,当需要修改一个url时,只需在controller定义接口的地方一处修改即可
2. url_for会为你处理转义
3. 如果你的应用程序是某个大应用的一个模块,url_for会自动为你处理,比如豆瓣小组,单独运行会是"/", 但如果放在豆瓣这个大的application下,url_for会生成"douban.com/group/"

代码:
with app.test_request_context():                                               
    print url_for('projects')                                                  
    print url_for('url', url='ab/c')                                           
    print url_for('url', url='ab/robert json')

输出:
/projects/
/people/ab/c
/people/ab/robert%20json

HTTP Methods:
@app.route("/login", methods=["GET", "POST"])                                  
def login():                                                                   
    if request.method=="GET":                                                  
        #show_login_form()                                                     
        pass                                                                   
    elif request.method=="POST":                                               
        #check_login_form()                                                    
        pass

methods:
GET: get information from body
HEAD: trigger get action, but only interested in header
POST: put data in server(and server trigger store action only once)
PUT: similar with POST, but can trigger POST many times, so it' ok even the transmission breaks.
DELETE: delete data
OPTIONS: this method is implemented automatically by Flask, for the browser to figure out which method(s) does the url supports.

Static File:
create a directory named static in your package or next to your modul, to generate urls for static files, use url_for:
url_for("static", filename="main.css")
the file has to be stored in fs as static/main.css

Rendering Templates:
generating HTML from python is not funny, but cumbersome because you have to make sure your application's secure.
use render_template and pass the template name and the context variables:
@app.route("/hello/<user_name>")                                               
def hello(user_name):                                                          
    return render_template('hello.html', user_name=user_name)

templates/hello.html:
<!doctype html>                                                                
                                                                               
hello, {{user_name}}

inside the template you can access the request/session/g object as well as get_flashed_messages() function.

use template, use template inheritance.

Accessing request data:
in Flask you can access the request data with global request object.
how's that possible?
proxy!

when the web server receives a request, it spawn a new thread,  when flask begin to deal with the request, it binds the current application and WSGI environments to that context(thread).

with flask, even in unittest it's easy for you to create a request:

with app.test_context_request('/hello', method='POST'):
    assert request.path == '/hello'
    assert request.method == 'POST'

the request object:
to get form data:
request.form['username']
but if the key doesn't exist in the form, Flask wil raise KeyError, Flask defaults it to 404.

to access parameters in url?key=value form, use request.args.get('key', default_value)


file uploading:
first set enctype="multipart/form-data"
use request.files to access the files, and request.files['form_name'] to access the specified file.
the file has filename attribute, but it can be forged, so use werkzeug.secure_filename(filename)

@app.route('/upload', methods=["GET", "POST"])                                 
def upload_resume():                                                           
    message = ''                                                               
    if request.method=="POST":                                                 
        file1 = request.files['file1']                                         
        if not file1:                                                          
            message = 'file not found'                                         
        else:                                                                  
            file1.save("/Users/liaofeng/python/flask/uploads/" +               
                    secure_filename(file1.filename))                           
            message = 'file saved'                                             
    return render_template('upload.html', message=message)


<!doctype html>                                                                                                                                     
{% if message %}                                                               
    {{message}}                                                                
{% endif %}                                                                    
                                                                               
<form action="/upload" method="POST" enctype="multipart/form-data">            
    select file: <input type="file" name="file1" />                            
    <input type="submit" value="submit"/>                                      
</form>


cookie:
access cookie: request.cookies, it's a dictionary
set cookie: response.set_cookie('name', 'value')
how to get response object:
1. if a controller method returns a string, Flask will automatically creates the response object.
2. use flask.make_response(response_string) method to explicitly create a response object.

but sometimes you might want to set cookie at the point response object doesn't exist, in this condition, use Deferred Request Callbacks pattern.

Redirect:
from flask import redirect
return redirect(url_for('login'))


abort a request earlier with a error code:
from flask import abort
abort(404) # notice: no return exists
this_will_never_execute()

or use errorhandle:

@app.errorhandle(404)
def page_not_found(error):
    return render_template('page_not_found_error.html'), 400
404 is the http response code, it defaults to 200

Response:
with the string returned from a controller method, it will be used as the response body, with http response code 200 and text/html mimetype.
if the return is a tuple, show be in form (response_body, code, headers)

Sessions:
store some information for some use from one request to the next.
session is implemented on top of cookie and set the cookie cryptographically,
is means use can look the cookie content but can't modify it unless he knows the secret key for signing.

from flask import Flask, request, render_template, session, redirect, url_for  
                                                                               
app = Flask(__name__)                                                          
app.debug = True                                                               
app.secret_key = '\xbbM\xfe\t@q\x11\xeb\xf2Y\x089\x8f\x07\xa6\x82q\x1e\x85RD\x92 \x93'
                                                                               
@app.route('/')                                                                   
def index():                                                                      
    return "user in session: %s" % session.get('username', 'none')                
                                                                                  
@app.route('/login', methods=["GET", "POST"])                                     
def login():                                                                      
    if 'username' in session:                                                     
        return redirect(url_for('index'))                                         
                                                                                  
    if request.method=="GET":                                                     
        return render_template("login.html")                                      
    else:                                                                         
        session['username'] = request.form['username']                            
        return redirect(url_for('index'))


@app.route('/logout')                                                             
def logout():                                                                  
    if 'username' in session:                                                  
        session.pop('username')                                                
    return redirect(url_for('login'))                                          
                                                                               
                                                                               
if __name__=='__main__':                                                       
    app.run()


how to generate secret key:
import os
os.urandom(24)

note: make sure the cookie and session below the browser's maximum capacity.

Message flashing:

Logging:
app.logger.debug()
app.logger.warning()
app.logger.error()

that's all, thank you.
分享到:
评论

相关推荐

    flash学习笔记.pdf

    flash学习笔记.pdf

    2023年Flash学习笔记.docx

    "2023年Flash学习笔记" FlashCS5是由美国Adobe公司推出的一款以矢量图绘制、编辑和动画制作为一体的专业软件。用它可以做网页动画、MTV、游戏、课件等等。 界面组成: 1. 标题栏 2. 菜单栏(集合了该软件的所有...

    nandflash学习笔记

    ### NAND Flash 学习笔记关键知识点解析 #### 一、NAND Flash 是否有统一接口标准? - **标准现状**:目前存在一个名为 ONFI (Open NAND Flash Interface) 的标准,旨在为 NAND Flash 设定统一的接口规范。然而,...

    FLASH学习笔记.doc编程资料

    FLASH学习笔记.doc

    Nor Flash 学习笔记

    在学习Nor Flash时,首先要了解它的基本工作模式。Nor Flash在上电后默认处于数据读取状态,可以直接像读取SDRAM、SRAM或ROM一样读取其中的数据。在进行任何其他操作之前,通常需要读取芯片的设备ID来确定程序是否...

    FLASH学习笔记.doc

    flash

    flash学习笔记(一些常用方法的例子)

    在Flash AS3编程中,`DisplayObject` 类是所有可显示对象的基础,包括图形、按钮、文本字段等。这个类提供了许多核心功能,允许开发者控制这些对象在舞台上的位置、大小以及与其他对象的交互。以下是关于`...

    2023年FLASH学习笔记.doc

    在学习2023年的Flash技术时,首先要了解Flash的基础概念和文件格式。Flash主要包含三种文献格式:FLA作为源文件,可以编辑动画内容;SWF是电影播放文件,仅用于观看;EXE则是可执行播放文件,无需依赖Flash Player。...

    Flash学习笔记.docx编程资料

    ### Flash CS5 学习笔记知识点汇总 #### 一、Flash CS5 软件介绍 - **定义**: Flash CS5 是一款由Adobe公司开发的专业软件,主要用于矢量图形的绘制、编辑以及动画制作。 - **应用领域**: 可用于制作网页动画、音乐...

    STM32增强型微控制器访问NAND Flash学习笔记

    STM32增强型微控制器访问NAND Flash学习笔记 本篇学习笔记主要介绍了STM32增强型微控制器访问NAND Flash的相关知识,涉及到Flash存储器的分类、NAND Flash的工作原理、HY27UF081G2A芯片的引脚功能和NAND Flash接口...

    FLASH__HCS12学习笔记(5)

    ### HCS12 Flash 学习笔记知识点梳理 #### 一、HCS12 Flash 基本概念 - **Flash 内存**: Flash 是一种非易失性存储器技术,即使断电后也能保留数据。在嵌入式系统中,Flash 主要用于存储程序代码和静态数据。 - *...

    Flash8学习笔记

    【Flash8学习笔记】 Flash8 是一款经典的动画创作软件,广泛应用于网页设计、游戏开发以及交互式内容制作。本笔记旨在记录学习过程中的一些关键知识点,同时也适用于初学者参考。 一、资源与工具 1. 动画成品分享...

    Flash 8学习笔记

    ### Flash 8学习笔记知识点详解 #### 一、基本概念及功能介绍 **Flash 8** 是一款由Adobe公司推出的多媒体创作工具,主要用于创建动画、交互式内容以及复杂的矢量图形。它支持多种媒体格式,如视频、音频、位图等...

    AS3学习笔记flashdevelop平台搭建配置.pdf

    AS3学习笔记FlashDevelop平台搭建配置 AS3学习笔记FlashDevelop平台搭建配置是关于如何搭建和配置FlashDevelop平台的学习笔记。下面是对该笔记中所涉及到的知识点的详细说明: 1. FlashDevelop平台简介 Flash...

    STM32 RTT学习笔记(三)SPI FLASH

    本文是关于STM32 RT-Thread操作系统中添加SPI Flash设备的学习笔记,主要涉及STM32F407vet6芯片和RT-Thread组件的SPI驱动及SFUD库的使用。首先,我们要了解SPI Flash的两种型号:W25X40CL和W25Q64。它们分别具有不同...

    小白Flash脚本学习笔记

    ### 小白Flash脚本学习笔记 #### 一、鼠标事件与监听 在Flash脚本中,处理鼠标事件是常见的需求之一。通过设置鼠标监听器,可以实现对鼠标移动、点击等行为的响应。 ##### 1.1 监听器创建 ```actionscript var ...

    flash殿堂之路学习笔记

    综上所述,这份学习笔记全面覆盖了ActionScript3的基础到高级知识,从语言基础到面向对象编程,再到核心类和API的使用,为读者提供了成为Flash开发高手所需的所有理论和实践指导。无论是初学者还是有一定经验的...

    flash语言学习笔记.pdf

    Flash 语言学习笔记 本资源摘要信息对应的文件名为 "flash 语言学习笔记.pdf",该文件主要介绍了 Flash 语言的基础知识和应用示例。下面是对该文件中所涉及到的知识点的详细解释: FLASH 语言基础 * On() 函数...

    MSP430学习笔记

    │ MSP430学习笔记系列8—比较器-Flash.pdf │ ├─MSP430学习笔记系列1—快速上手MSP430 │ hello_第一个程序源代码.rar │ MSP430学习笔记系列1—快速上手MSP430.pdf │ └─MSP430学习笔记系列2—基本程序框架...

Global site tag (gtag.js) - Google Analytics