Immediately after becoming independent as a freelancer, I decided to make a LINE bot at work. I started self-education because I wanted to implement it in Python, which is said to be good at artificial intelligence and natural language processing, and give it extensibility. I'm new to Python.
Before the detailed bot specifications were decided, I learned the following for self-education. https://www.udemy.com/course/python-flask-python-api-line-bot/
However, Python was only learned by dot installation and Progate, and I had no knowledge of the framework, so it became "Flask?". In this article, I thought I'd put together Flask for myself. I'm really a beginner, so please point out any differences. ..
The point is that it is difficult to write Python by hand and create a web page, so it seems that you should easily proceed with development using a framework that can be shared.
Reference: https://dividable.net/python/flask-python/
It claims to be a "microweb application framework" and seems to have only the minimum necessary functions.
Rough comparison with Django, the same web framework ↓
Flask | Django | |
---|---|---|
weight | Light | Heavy |
Customizability | high | Not suitable for detailed customization |
Initial setup | early | Various preparations are required |
Learning cost | Low because there are few functions | I can do various things, but it seems to be quite difficult |
Suitable business | Single-function Web application / business management screen | Large-scale service for enterprises |
Because it looks like this, it seems that Flask is more suitable when making a bot. I want you to get a reply quickly!
Please refer to Official for installation and environment construction.
After installation, just write the following and it will work as a Web service. It's that easy! The author has been trained to write Java, so I was impressed that it was dizzyingly easy.
python
from flask import Flask #Import Flask class
app = Flask(__name__) #Declare the name of your Flask application
@app.route('/') #Set the URL to launch the function
def hello_world(): #Function definition
return "Hello World!" #This time in the browser"Hello World!"To display
if __name__ == '__main__': #When started as an application
app.run() #Run the application on the local server
What is characteristic is ʻapp = Flask (__ name__)and
name =='main'`.
It took me a long time to understand here.
__name__
?First, let's talk about the module name of this application. If you use only one module, you have to use
__name__
. This is because the name is different when it is launched as an application and when it is imported as a module ('__ main__'
when launched as an application, its import name when imported).
ʻIf name =='main': `guarantees that it will only run on that server when the script is run directly from the Pyrhon interpreter, not when it is imported as a module.
Officially, it is described as above. Hmm, I see?
__name__
is an automatically defined variable that contains the module name of the current file.
Suppose you've created three Flask applications (let's name them One, Two, Three) Consider the case where One and Two are imported by Three.
Suppose that each of the three has a description of ʻif name =='main': app.run () `.
For example, when running Three on the server, by replacing __name__
with the appropriate name,
It seems that One and Two are designed not to run on the server.
(The module name at this time is that Three is treated as __main__
, One is treated as ʻOne, and Two is treated as
Two`.)
Now, let's execute the created hello.py.
$ python hello.py
Flask has a built-in local development server, so
If you run the above from the command line, you can access it on localhost port 5000 (http://127.0.0.1:5000/
).
Let's open a web browser and access the URL. It is OK if "Hello World" is displayed in the upper left corner. You are now ready to use Flask. You did it!
If you are using a virtual environment such as Docker, access the corresponding address and port. (I might write another article soon ...)
To stop the server, press control-C.
After I was able to do this, I tried making a bot for Echolalia, and then proceeded to development for work. Various people have written articles on how to make a parrot return bot, so if you look at that, I think that you can proceed without difficulty.
Next time, I'll start with a database-related article that was messed up during work development. Please take a look if you are interested. ..
https://a2c.bitbucket.io/flask/installation.html#installation (official) https://www.udemy.com/course/python-flask-python-api-line-bot/ https://methane.github.io/flask-handson/start.html
Recommended Posts