Build a WebAPI server with python3 so that you can check the operation of the front desk. Remarks
Installation complete
>$ python3 --version
Python 3.7.7
$ python3 -m pip install Flask
Collecting Flask
Downloading Flask-1.1.2-py2.py3-none-any.whl (94 kB)
|████████████████████████████████| 94 kB 792 kB/s
Collecting Jinja2>=2.10.1
Downloading Jinja2-2.11.2-py2.py3-none-any.whl (125 kB)
|████████████████████████████████| 125 kB 6.5 MB/s
Collecting Werkzeug>=0.15
Downloading Werkzeug-1.0.1-py2.py3-none-any.whl (298 kB)
|████████████████████████████████| 298 kB 3.8 MB/s
Collecting itsdangerous>=0.24
Downloading itsdangerous-1.1.0-py2.py3-none-any.whl (16 kB)
Collecting click>=5.1
Downloading click-7.1.2-py2.py3-none-any.whl (82 kB)
|████████████████████████████████| 82 kB 778 kB/s
Collecting MarkupSafe>=0.23
Downloading MarkupSafe-1.1.1-cp37-cp37m-macosx_10_6_intel.whl (18 kB)
Installing collected packages: MarkupSafe, Jinja2, Werkzeug, itsdangerous, click, Flask
Successfully installed Flask-1.1.2 Jinja2-2.11.2 MarkupSafe-1.1.1 Werkzeug-1.0.1 click-7.1.2 itsdangerous-1.1.0
>$ python3
>>> import flask
>>> flask.__version__
'1.1.2'
hello.py
from flask import Flask
from flask import jsonify
app = Flask(__name__)
@app.route('/')
def hello():
return jsonify({"message": "Hello"})
@app.route('/good')
def good():
return jsonify({"message": "Good"})
if __name__ == "__main__":
app.run(debug=True)
$ python3 hello.py
You can also check it from Chrome's developer tools. If you switch the commented out variable url and execute it, You can see that the content of the response changes.
//let url = 'http://127.0.0.1:5000/';
let url = 'http://127.0.0.1:5000/good';
let req = new XMLHttpRequest();
req.open('GET', url);
req.onreadystatechange = function() {
if (req.readyState == 4 && req.status == 200) {
console.log("XMLHttpRequest");
//let data = eval('(' + req.responseText + ')');
console.log(req.responseText);
alert(JSON.parse(req.responseText).message);
}
};
req.send(null);
Launch API server and Web server quickly with Python standard library Hello World output from Flask! Installation in 1 minute How to check the version of Python framework Flask First Flask # 5 ~ Let's write a Web API that returns JSON ~ jsonify is not defined-internal server error [request has no attribute'form' when receiving POST-sent parameters in Flask](https://qiita. com / bowtin / items / 194c1f3b1211d7892ab8) Flask-Response Objects