Flask: Difference between revisions
No edit summary |
(→Opions) |
||
(6 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
[[Category:Scripting]] |
|||
__TOC__ |
|||
<br /> |
|||
= Basics = |
|||
Source: [https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world blog.miguelgrinberg.com] |
|||
Installation: |
Installation: |
||
pip3 install flask |
pip3 install flask |
||
Hello World |
== Hello World == |
||
<syntaxhighlight lang="python"> |
<syntaxhighlight lang="python"> |
||
from flask import Flask |
from flask import Flask |
||
Line 15: | Line 22: | ||
</syntaxhighlight> |
</syntaxhighlight> |
||
Access the above app from: |
*Access the above app from: |
||
http://127.0.0.1:5000/ |
http://127.0.0.1:5000/ |
||
== Parameters == |
|||
⚫ | |||
*All parameters are optional: |
|||
app.run(host, port, debug, options) |
|||
Explanation: |
|||
host: Hostname to listen on. Defaults to 127.0.0.1 (localhost). Set to ‘0.0.0.0’ to have server available externally |
|||
port: Defaults to 5000 |
|||
debug: Defaults to false. If set to true, provides a debug information |
|||
options: To be forwarded to underlying Werkzeug server. |
|||
⚫ | |||
app.debug = True |
app.debug = True |
||
Access URL over network with custom port: |
*Access URL over network with custom port: |
||
app.run(host = '0.0.0.0',port=5005) |
app.run(host = '0.0.0.0',port=5005) |
||
= Uploader Project = |
|||
Source: [https://www.tutorialspoint.com/flask/flask_file_uploading.htm tutorialspoint.com] |
|||
nano upload_test.py |
|||
<syntaxhighlight lang="python"> |
|||
from flask import Flask, render_template, request |
|||
from werkzeug import secure_filename |
|||
app = Flask(__name__) |
|||
@app.route('/upload') |
|||
def upload_file(): |
|||
return render_template('upload.html') |
|||
@app.route('/uploader', methods = ['GET', 'POST']) |
|||
def upload_file(): |
|||
if request.method == 'POST': |
|||
f = request.files['file'] |
|||
f.save(secure_filename(f.filename)) |
|||
return 'file uploaded successfully' |
|||
if __name__ == '__main__': |
|||
app.run(debug = True, host = '0.0.0.0') |
|||
</syntaxhighlight> |
|||
mkdir templates && cd templates |
|||
nano upload.html |
|||
<syntaxhighlight lang="html"> |
|||
<html> |
|||
<body> |
|||
<form action = "http://10.10.10.1:5000/uploader" method = "POST" |
|||
enctype = "multipart/form-data"> |
|||
<input type = "file" name = "file" /> |
|||
<input type = "submit"/> |
|||
</form> |
|||
</body> |
|||
</html> |
|||
</syntaxhighlight> |
|||
File Structure: |
|||
. |
|||
├── templates |
|||
│ └── upload.html |
|||
└── upload_test.py |
|||
Run the application: |
|||
python3 upload_test.py |
|||
Access the application on below URL: |
|||
http://10.10.10.1:5000/upload |
|||
<br /> |
|||
;References |
|||
<references/> |
|||
<br /> |
|||
<br /> |
|||
<br /> |
|||
{{DISQUS}} |
Latest revision as of 12:25, 14 June 2018
Basics
Source: blog.miguelgrinberg.com
Installation:
pip3 install flask
Hello World
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello World’
if __name__ == '__main__':
app.run()
- Access the above app from:
http://127.0.0.1:5000/
Parameters
- All parameters are optional:
app.run(host, port, debug, options)
Explanation:
host: Hostname to listen on. Defaults to 127.0.0.1 (localhost). Set to ‘0.0.0.0’ to have server available externally port: Defaults to 5000 debug: Defaults to false. If set to true, provides a debug information options: To be forwarded to underlying Werkzeug server.
- Enable Debugging:
app.debug = True
- Access URL over network with custom port:
app.run(host = '0.0.0.0',port=5005)
Uploader Project
Source: tutorialspoint.com
nano upload_test.py
from flask import Flask, render_template, request
from werkzeug import secure_filename
app = Flask(__name__)
@app.route('/upload')
def upload_file():
return render_template('upload.html')
@app.route('/uploader', methods = ['GET', 'POST'])
def upload_file():
if request.method == 'POST':
f = request.files['file']
f.save(secure_filename(f.filename))
return 'file uploaded successfully'
if __name__ == '__main__':
app.run(debug = True, host = '0.0.0.0')
mkdir templates && cd templates nano upload.html
<html>
<body>
<form action = "http://10.10.10.1:5000/uploader" method = "POST"
enctype = "multipart/form-data">
<input type = "file" name = "file" />
<input type = "submit"/>
</form>
</body>
</html>
File Structure:
. ├── templates │ └── upload.html └── upload_test.py
Run the application:
python3 upload_test.py
Access the application on below URL:
http://10.10.10.1:5000/upload
- References
{{#widget:DISQUS
|id=networkm
|uniqid=Flask
|url=https://aman.awiki.org/wiki/Flask
}}