Skip to content

Instantly share code, notes, and snippets.

@always-hii
Last active May 9, 2020 07:45
Show Gist options
  • Save always-hii/5ebbca9aa39c74a3dbfc5ec2bf46c54f to your computer and use it in GitHub Desktop.
Save always-hii/5ebbca9aa39c74a3dbfc5ec2bf46c54f to your computer and use it in GitHub Desktop.
[Recipe of Code] GET /makers/{mid}
import pandas as pd
import pymysql
from flask import Flask, request
from flask_restful import Api, Resource
host = 'your_mysql_ip'
user = 'your_username'
password = 'your_password'
database = 'rest'
class Maker(Resource):
""" /makers/{mid} """
def get(self, mid):
query = f'SELECT * FROM rest.Makers where id={mid}'
conn = pymysql.connect(host=host, user=user, password=password, database=database)
dataframe = pd.read_sql(sql=query, con=conn)
conn.close()
maker = dataframe.to_dict('record')[0]
ret = { 'data': maker }
return ret, 200
if __name__ == '__main__':
app = Flask(__name__)
api = Api(app)
api.add_resource(Maker, '/makers/<mid>')
app.run(host='0.0.0.0', port=5555)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment