Install version 3.6.8 from pkg [How to install from pkg file](https://qiita.com/ms-rock/items/72b8f1abc661c539bb09#21-pkg%E3%83%95%E3%82%A1%E3%82%A4%E3%83 % AB% E3% 81% 8B% E3% 82% 89% E3% 81% AE% E3% 82% A4% E3% 83% B3% E3% 82% B9% E3% 83% 88% E3% 83% BC % E3% 83% AB% E6% 96% B9% E6% B3% 95)
# Python3.6.Check the installation location of 8
$ which python
#Install python with brew to install pip3
$ brew install python
#virtualenv installation
$ pip3 install virtualenv
#Create virtual environment
$ virtualenv --python=/Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6 env
#Version confirmation
$ python
Python 3.6.8 (v3.6.8:3c6b436a57, Dec 24 2018, 02:04:31)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
How to get inside the virtual environment
source bin/activate
Python3 installation (Mac version)
--Create a Line Developer account --Creating a channel --Bot creation
Follow the steps below to "Create a LINE bot with Flask". [Until you implement LINE bot in Flask and deploy to heroku](https://qiita.com/suigin/items/0deb9451f45e351acf92#%E9%96%8B%E7%99%BA%E7%92%B0%E5 % A2% 83)
Run from the beginning of the virtual environment.
$ source bin/activate
$ pip install flask
$ pip install line-bot-sdk
$ brew cask install ngrok
SDK to use: line-bot-sdk-python What is homebrew-cask
pip3 install python-dotenv
Best practices for configuration files in Flask How to write environment variables that you don't want to put on [GitHub] Python
Add the following to ʻapp.py, access the root path, and confirm that
hello world! `is displayed.
@app.route("/")
def hello_world():
return "hello world!"
flask execution command
flask run --host=0.0.0.0
ngrok
If you want to test Line, you need to enter the test URL in the webhook.
At this time, the URL of https
is required, and ngrok
is convenient at that time.
You can publish your local development on https. Please check below for details. ngrok is too convenient
In a separate terminal, do the following
flask run --host=0.0.0.0
ngrok http 5000
Heroku
$ brew install heroku/brew/heroku
#The browser will be displayed, so log in there
$ heroku login
Create the following three new
requirements.txt
Use feeze
to export the version you are currently in the virtual environment to requirements.txt.
Check the list of installed packages with Python, pip list / freeze
runtime.txt Describe the Python version.
Procfile
web: gunicorn app:app --log-file -
About WSGI Differences between web server and application server in Rails development (translation)
Nginx passes the request to Unicorn. Unicorn passes the request to Rack. Rack passes the request to the Rails router. The router passes the request to the appropriate controller. Then the responses are returned in reverse order.
heroku create [app name]
git push heroku master
#List of environment variables
$ heroku config
#Add environment variable
$ heroku config:set ENV_NAME=value
[Heroku command] environment variables
heroku run bash
Heroku Command Collection
By implementing the following, you can define the action when Beacon Event occurs.
If you want to change the event, change the argument (BeaconEvent
) of@ handler.add (BeaconEvent)
.
So, the program below should describe how to handle ** after the event. ** **
The event occurrence condition is implemented on the Line side.
#Beacon event fired
@handler.add(BeaconEvent)
def handle_beacon(event):
line_bot_api.reply_message(
event.reply_token,
TextSendMessage(text='Hello World'))
The above implementation uses a decorator.
By using a decorator, you can add a process that is automatically executed when you call a function provided as a library. Quoted from About Python Decorators
handler = WebhookHandler(channel_secret)
#Channel the WebhookHandler class_Initialize with secret
handler.handle(body, signature)
##behavior of handle method
#Execution of handle method of WebhookHandler class
#Execute parse method of WebhookParser class
#Since the body contains events, append to events and return events
#Turn events with a for statement
##decorator behavior
#add method is called
#Returns a decorator
#At this time,app.handle implemented in py_message method is called
#In the decorator method__add_The handler method is called_There is a value in handlers
#The add method of the WebhookHandler class is decorated and the decorator is called
# __add_Called handler
##I don't know yet
#I don't know where the add method is running
# handle_I can execute it without calling the message method, so I do not know where it is executed in the handle method
The following will be helpful. Procedure to start Flask + SQLAlchemy project
It will automatically generate a migration file from the model file. In my own words about Flask-Migrate
Run locally
#Sets the database revision to the revision specified as an argument without performing the migration.
$ flask db stamp head
$ flask db migrate
$ flask db upgrade
heroku run FLASK_APP=app.py flask db stamp head
heroku run FLASK_APP=app.py flask db migrate
heroku run FLASK_APP=app.py flask db migrate
Recommended Posts