Flask: Difference between revisions
Content added Content deleted
Line 25: | Line 25: | ||
= Uploader Project = |
= Uploader Project = |
||
nano upload_test.py |
|||
<syntaxhighlight lang="python"> |
<syntaxhighlight lang="python"> |
||
Line 46: | Line 48: | ||
</syntaxhighlight> |
</syntaxhighlight> |
||
mkdir templates |
mkdir templates && cd templates |
||
nano upload.html |
nano upload.html |
||
Line 62: | Line 64: | ||
</html> |
</html> |
||
</syntaxhighlight> |
</syntaxhighlight> |
||
File Structure: |
|||
. |
|||
├── templates |
|||
│ └── upload.html |
|||
└── upload_test.py |
|||
Run the application: |
|||
python3 upload_test.py |
|||
Access the application on below URL: |
Access the application on below URL: |
Revision as of 15:27, 13 June 2018
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/
Enable Debugging:
app.debug = True
Access URL over network with custom port:
app.run(host = '0.0.0.0',port=5005)
Uploader Project
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