Build API server for checking the operation of front implementation with python3 and Flask

Overview

Build a WebAPI server with python3 so that you can check the operation of the front desk. Remarks

Premise

Installation complete

>$ python3 --version
Python 3.7.7

Flask installation

$ 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

Version confirmation

>$ python3
>>> import flask
>>> flask.__version__
'1.1.2'

Source

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

Request from the front desk and confirm

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); 

reference

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

Recommended Posts

Build API server for checking the operation of front implementation with python3 and Flask
Code for checking the operation of Python Matplotlib
Crawling with Python and Twitter API 2-Implementation of user search function
Get the number of articles accessed and likes with Qiita API + Python
I measured the speed of list comprehension, for and while with python2.7.
Launch a web server with Python and Flask
Implementation of TRIE tree with Python and LOUDS
Deep Learning from scratch The theory and implementation of deep learning learned with Python Chapter 3
Build a python environment to learn the theory and implementation of deep learning
Visualize the range of interpolation and extrapolation with python
Implementation example of LINE BOT server for actual operation
Get the operation status of JR West with Python
A python script that generates a sample dataset for checking the operation of a classification tree
Automate background removal for the latest portraits in a directory with Python and API
Call the API with python3.
Try hitting the Twitter API quickly and easily with Python
Check the operation of Python for .NET in each environment
Sample image of Python API server for EC2 (public AMI)
Let's touch the API of Netatmo Weather Station with Python. #Python #Netatmo
Build a Python environment and transfer data to the server
Build a speed of light web API server with Falcon
Summary of Hash (Dictionary) operation support for Ruby and Python
Try to automate the operation of network devices with Python
Library for specifying a name server and dig with python
Play with the password mechanism of GitHub Webhook and Python
Return the image data with Flask of Python and draw it to the canvas element of HTML
[Sakura Rental Server] (For beginners) How to build an environment for Python, pyenv, and Flask. | For csh
I compared the speed of Hash with Topaz, Ruby and Python
Build and test a CI environment for multiple versions of Python
Aggregate the number of hits per second for one day from the web server log with Python
[Python] Organize the basic structure of Flask apps (Aim for de-copying)
Implementation of CRUD using REST API with Python + Django Rest framework + igGrid
Build a detonation velocity website with Cloud Run and Python (Flask)
Persist Flask API server with forever
[Python] I thoroughly explained the theory and implementation of logistic regression
The story of making a standard driver for db with python.
Hit the Etherpad-lite API with Python
[Python] I thoroughly explained the theory and implementation of decision trees
Implementation of Dijkstra's algorithm with python
The story of returning to the front line for the first time in 5 years and refactoring Python Django
Coexistence of Python2 and 3 with CircleCI (1.0)
Set up a dummy SMTP server in Python and check the operation of sending from Action Mailer
Get the width of the div on the server side with Selenium + PhantomJS + Python
I replaced the numerical calculation of Python with Rust and compared the speed
Calculate the shortest route of a graph with Dijkstra's algorithm and Python
I tried to get the authentication code of Qiita API with Python.
Hit a method of a class instance with the Python Bottle Web API
Find the general terms of the Tribonacci sequence with linear algebra and Python
I tried to get the movie information of TMDb API with Python
Edit Slide (PowerPoint for Google) with Python (Low-cost RPA case with Google API and Python)
Get and estimate the shape of the head using Dlib and OpenCV with python
Explanation of creating an application for displaying images and drawing with Python
I made go language for api and minimum configuration of react for front
Build a lightweight server in Python and listen for Scratch 2 HTTP extensions
Check the existence of the file with python
POST variously with Python and receive with Flask
[python] Checking the memory consumption of variables
The third night of the loop with for
Pandas of the beginner, by the beginner, for the beginner [Python]
[Python] Quickly create an API with Flask
The second night of the loop with for