Python Flask on Heroku
Playground 1
5. Create a sample Program to verify on local
$ python app.py
* Running on http://localhost:5000/
Running Flask on local
6. Change Code to work on Heroku
7. Declare the depencies
$ pip freeze > requirements.txt
8. Specify the Procfile
A Procfile is a text file in the root directory of your application that defines process types and explicitly declares what command should be executed to start your app.
$vi Procfile
Insert this line:
web: python app.py
9. Login to Heroku
$ heroku login
10. Create the app on Heroku
$ heroku create
11. Start Deployment
$ git add.
$ git commit -m "new python"
$ git push heroku master
12. That's it! Now run your service
$ heroku open
How to create a Flask service on Heroku
1. Install the Virtual Env
$ pip install virtualenv
2. Set up a virtual Environment - This limits the dependencies you needed to run the project
$ virtualenv venv
3. Activate Virtrual environment
$ source venv/bin/activate
1. Install the Virtual Env
$ pip install virtualenv
2. Set up a virtual Environment - This limits the dependencies you needed to run the project
$ virtualenv venv
3. Activate Virtrual environment
$ source venv/bin/activate
4. Install Flask
$ pip install Flask5. Create a sample Program to verify on local
$ python app.py
* Running on http://localhost:5000/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask | |
app = Flask(__name__) | |
@app.route("/") | |
def hello(): | |
return "Welcome to Python Flask!" | |
if __name__ == "__main__": | |
app.run() |
For Heroku, you need to specify the port and the environment
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask | |
app = Flask(__name__) | |
@app.route("/") | |
def hello(): | |
return "Welcome to Python Flask!" | |
if __name__ == "__main__": | |
#app.run() | |
# Bind to PORT if defined, otherwise default to 5000. | |
# Needed to run using Heroku | |
port = int(os.environ.get('PORT', 5000)) | |
app.run(host='0.0.0.0', port=port) |
7. Declare the depencies
$ pip freeze > requirements.txt
8. Specify the Procfile
A Procfile is a text file in the root directory of your application that defines process types and explicitly declares what command should be executed to start your app.
$vi Procfile
Insert this line:
web: python app.py
9. Login to Heroku
$ heroku login
10. Create the app on Heroku
$ heroku create
11. Start Deployment
$ git add.
$ git commit -m "new python"
$ git push heroku master
12. That's it! Now run your service
$ heroku open
コメント
コメントを投稿