[PYTHON] code-server Local environment (9) Let's prepare an MVC environment including DB (6)

This is the 9th day article of Advent Calender on the 2019 code-server. This time as well, I will explain what is code-server?

(1) What is code-server? (2) Create your own code-server environment with Docker (3) Try using VSCode Plugin (4) Let's prepare an MVC environment including DB etc. (1) (5) Let's prepare an MVC environment including DB etc. (2) (6) Let's prepare an MVC environment including DB etc. (3) (7) Let's prepare an MVC environment including DB etc. (4) (8) Let's prepare an MVC environment including DB etc. (5) (9) Let's prepare an MVC environment including DB etc. (6) (10) Bonus

(NEXT->) Online environment version 1st day Improve the work environment

(..) To build locally including environment such as DB (..) How to put it online? (..) How to work with the latest trendy environment such as K8S? (..) I want to modify Code-Server to make it better

Continuing from the last time, we will add APIs using flask.

What is this time?

Terminal can start multiple at the same time

Decide what kind of API

Let's modify the previously created flask

main.py


from flask import Flask, request as fl_request, Request
from typing import Dict
import json
import logging

app = Flask(__name__)
logger = logging.getLogger("XXX")
logging.basicConfig(level=logging.DEBUG)

@app.route("/users")
def get_user_from_id():
    request:Request = fl_request
    input:Dict = request.args
    logger.debug("> input: {}".format(input))
    user:Dict = {
        "name":"one",
        "email":"[email protected]",
        "id":1}
    return json.dumps(user)

app.run("0.0.0.0",port=8080)

If an ID is passed as http://0.0.0.0/users?id=1, the user information corresponding to that ID will be returned.

Try to move

Try starting flask.


$ python main.py 
 * Serving Flask app "main" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
INFO:werkzeug: * Running on http://0.0.0.0:8080/ (Press CTRL+C to quit)

Open a new terminal and try hitting the curl command

$ curl 127.0.0.1:8080/users?id=1
{"name": "one", "email": "[email protected]", "id": 1}

Oops, user information has been given.

Read data from DB and display it

This time, I am using the dataset package to operate db from python. https://github.com/pudo/dataset

$ pip install dataset

Since we will use mysql, add the mysqlclient package.

$ pip install mysqlclient

main.py


from flask import Flask, request as fl_request, Request
from typing import Dict
import json
import dataset
import logging

app = Flask(__name__)
logger = logging.getLogger("XXX")
logging.basicConfig(level=logging.DEBUG)

db:dataset.Database = dataset.connect('mysql://root:passwd@mysqld/app_db')

@app.route("/users")
def get_user_from_id():
    request:Request = fl_request
    input:Dict = request.args
    logger.debug("> input: {}".format(input))
    users_table:dataset.Table = db.get_table("users")
    user:Dict = users_table.find_one(id=int(input["id"]))
    return json.dumps(user)

app.run("0.0.0.0", port=8080)

You can connect to the database with dataset.connect and get user information with ʻusers_table.find_one`.

Try to move

Try starting flask.


$ python main.py 
 * Serving Flask app "main" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
INFO:werkzeug: * Running on http://0.0.0.0:8080/ (Press CTRL+C to quit)

Open a new terminal and try hitting the curl command

$ curl 127.0.0.1:8080/users?id=1
{"name": "one", "email": "[email protected]", "id": 1}

Oops, user information has been given.

PS

Let's create requirements.txt !!

It is customary to keep python packages in a file. https://pip.readthedocs.io/en/1.1/requirements.html

pip install -r requirements.txt

-r Add and run command line options.

requirements.txt


Flask==1.1.1
dataset==1.1.2
mysqlclient==1.4.6

You can also write a list of dependent packages to a file with pip freeze> requirements_lock.txt.

next time

In Flask, try adding user. Also, I plan to finish the local environment version in about two times and enter the online environment version and the magic remodeling version.

PS Source https://github.com/kyorohiro/advent-2019-code-server

Recommended Posts

code-server Local environment (9) Let's prepare an MVC environment including DB (6)
code-server Local environment (4) Let's prepare an MVC environment including DB (1)
code-server Local environment (5) Let's prepare an MVC environment including DB (2)
code-server online environment (6) Let's automate
code-server Local environment (3) Try using VSCode Plugin
Introduction to Python Let's prepare the development environment