Five Ways flask return data

. 1      Import OS
 2      from Flask Import the Flask, the render_template, the redirect, jsonify, the send_file
 . 3      App = the Flask ( the __name__ )
 . 4      
. 5      # development debug mode is turned on, the parameters may be provided directly app.run () in
 . 6      # app.debug = true 
. 7      # the app.config [ 'the DEBUG'] = true 
. 8      
. 9      
10    # (. 1) Flask similar return in the return HttpResponse django () . 11 # return directly back to the text, the string may return after 1.1.1 , dictionaries, and other tuples, 12 is # of The return type MUST BE A String, dict, tuple, the Response instance, or the WSGI Callable 13 is      @ app.route ( ' /  
     
     
' )
 14      DEF Home ():
 15          return   ' first_flask ' 
16      
. 17      # @ app.route (' / ') 
18 is      # DEF Home (): 
. 19      #      return {' Key ':' first_flask '} 
20 is      
21 is     # (2) return render_template flask in () return render () is similar to django
 22      # return render_teplate () returns a static file page 
23      @ app.route ( ' / index ' )
 24-      DEF index ():
 25          return render_template ( ' index.html ' )
 26     
27      
28     # return the redirect (. 3) in Flask () similar to the return redirect django () 302 temporary redirection
 29      # return the redirect () redirection request 
30      @ app.route ( ' / Reback ' )
 31 is      DEF Reback () :
 32          return the redirect ( ' / index ' )
 33 is      
34 is      
35      
36      # (. 4) in Flask jsonify () supports direct data transmission type json
 37 [      @ app.route ( ' / flask_json ' )
 38 is      DEF flask_json ():
 39          return jsonify ([ ' A ' , 2 ])
40      
41 is      
42 is      # (. 5) in the return send_file Flask () can return directly to the file
 43      # rear send_file will automatically identify the returned documents, or unidentified class browser will not resolve Download 
44 is      @ app.route ( ' / flask_file ' )
 45      DEF flask_file ():
 46 is          filepath = the os.path.join (os.path.dirname (os.path.abspath with ( __FILE__ )), ' File ' )
 47          filename = ' 1.png '       # the Type-the Content: Image / PNG 
48          # filename = '1.mp3' # the Content-the Type: Audio / MPEG 
49          # filename = '1.mp4' the Type-the Content #: Video / MP4
50         # filename = '1.pdf'    #Content-Type: application/pdf
51         # filename = '1.pptx'   #Content-Type: application/vnd.openxmlformats-officedocument.presentationml.presentation
52         # filename = '1.docx'   #Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document
53         # filename='1.zip'      #Content-Type: application/x-zip-compressed
54         # filename='1.rar'      #Content-Type: application/octet-stream
55         file=os.path.join(filepath,filename)
56         return send_file(file)
57     
58     
59     if __name__ == ' __Main__ ' :
 60          #flak service default port 5000, can be specified by the parameter
 61 is          # app.run () 
62 is          app.run (= Host ' 192.168.16.14 ' , Port = 8888, Debug = True)

 

 

Guess you like

Origin www.cnblogs.com/open-yang/p/11165554.html