Rubyist tried to make a simple API with Python + bottle + MySQL

python-framework.png

background

I usually develop in Ruby, but as the number of big data analysis jobs has increased, it became necessary to play with python, so first of all, a system with a simple API-like role with a lightweight framework bottle I decided to make it.

Miscellaneous feelings

It's insanely lightweight and easy, so you can use only the good parts of python and use the main language.

Overview of what I made

① Hit the API from the ruby application (2) Get the data in MySQL with the application written in python ③ Calculate with python ④ Return the calculation result to the ruby application

Implementation

    1. Create a virtual environment with virtualenv
pip install virtualenv
virtualenv bottle_sample
cd bottle_sample
source bin/activate
  1. Install bottle
pip install bottle
    1. Update data with / update

score_calculator.py


# -*- coding: utf-8 -*-
from bottle import route, run
import networkx as nx

db_name = {DATABASE name}
host = {DATABASE host}
username = {MySQL username}
passwd = {MySQL password}

@route('/update')
def update
  g = nx.DiGraph()
  import mysql.connector
  connect = mysql.connector.connect(db=db_name, host=host, port=3306, user=username, passwd=passwd)

  cur=connect.cursor()
  cur.execute("select from_user_id,to_user_id from awesome_graph_data)                                                                                      
  rows = cur.fetchall()
  for row in rows:
    if row[0] is not None:
      g.add_node(row[0])
      g.add_node(row[1])
      g.add_edge(row[0],row[1])

    cur.close()
    connect.close()
  pr=nx.pagerank(g,alpha=0.85,personalization=None, max_iter=500)
  
  for id, score in pr.items():
    print id,score #I will omit it this time, but you can save it in mysql here
  return str(pr)

Now you can calculate the pagerank from the directed graph and save it in mysql.

  1. Get score with / score / {user_id}

score_calculator.py


@route('/score/<user_id>')
def score
  import mysql.connector
  connect = mysql.connector.connect(db=db_name, host=host, port=3306, user=username, passwd=passwd)

  cur=connect.cursor()
  cur.execute("select score from scoda_data where user_id = {{user_id}}")                                                                                      
  score = cur.fetchall()
  return score

Now you can get the score of a specific user_id.

Summary

A very simple application can be easily created with python + bottle. You can also easily create a view file to create a web supplement.

I thought it would be interesting to go out with various calculations that didn't go well with ruby.

Recommended Posts

Rubyist tried to make a simple API with Python + bottle + MySQL
I tried to make a todo application using bottle with python
I tried to make a simple mail sending application with tkinter of Python
I tried to make a simple image recognition API with Fast API and Tensorflow
I tried to make a Web API
[2nd] I tried to make a certain authenticator-like tool with python
[3rd] I tried to make a certain authenticator-like tool with python
I tried to make a periodical process with Selenium and Python
I tried to make a 2channel post notification application with Python
[4th] I tried to make a certain authenticator-like tool with python
[1st] I tried to make a certain authenticator-like tool with python
I want to make a game with Python
Try to make a "cryptanalysis" cipher with Python
[Python] Make a simple maze game with Pyxel
Try to make a dihedral group with Python
Python: I tried to make a flat / flat_map just right with a generator
I tried to make "Sakurai-san" a LINE BOT with API Gateway + Lambda
I tried to make a traffic light-like with Raspberry Pi 4 (Python edition)
Try to make a command standby tool with python
I tried to draw a route map with Python
Make a simple Slackbot with interactive button in python
[Patent analysis] I tried to make a patent map with Python without spending money
Let's make a simple game with Python 3 and iPhone
I tried to make a castle search API with Elasticsearch + Sudachi + Go + echo
Machine learning beginners tried to make a horse racing prediction model with python
Make a fortune with Python
I tried to automatically generate a password with Python3
What is God? Make a simple chatbot with python
[Vagrant] Set up a simple API server with python
I tried to make a real-time sound source separation mock with Python machine learning
[Mac] I want to make a simple HTTP server that runs CGI with Python
I tried to make various "dummy data" with Python faker
The first API to make with python Djnago REST framework
Experiment to make a self-catering PDF for Kindle with Python
How to make a simple Flappy Bird game with pygame
[Python] Make a simple maze game with Pyxel-Make enemies appear-
I tried to make a stopwatch using tkinter in python
I tried to make GUI tic-tac-toe with Python and Tkinter
I tried to make a simple text editor using PyQt
Let's make a GUI with python.
Make a recommender system with python
Let's make a graph with python! !!
[1 hour challenge] I tried to make a fortune-telling site that is too suitable with Python
I tried to create a RESTful API by connecting the explosive Python framework FastAPI to MySQL.
I tried to make a generator that generates a C # container class from CSV with Python
A story about adding a REST API to a daemon made with Python
I tried to make a regular expression of "amount" using Python
[Python] I tried to implement stable sorting, so make a note
I tried to make a regular expression of "time" using Python
[Python] A memo that I tried to get started with asyncio
How to make a surveillance camera (Security Camera) with Opencv and Python
Make a simple OMR (mark sheet reader) with Python and OpenCV
I tried to create a list of prime numbers with python
I tried to make a regular expression of "date" using Python
Make a request from Device Farm (appium python) to API Gateway
[Introduction] I want to make a Mastodon Bot with Python! 【Beginners】
[Python] Simple Japanese ⇒ I tried to make an English translation tool
I tried to make a strange quote for Jojo with LSTM
How to send a request to the DMM (FANZA) API with python
I tried to make an image similarity function with Python + OpenCV
I tried to make a mechanism of exclusive control with Go