Hit a method of a class instance with the Python Bottle Web API

I'm new to Python.

When I started making a web application because I could easily make a web application with Bottle, I was addicted to hitting the method of the instance.

Most commentary sites have the following description.

@route('/api')
def api_action():
    ...

I wanted to hit the method of a specific class instance in this api_action, but beginners got hooked here.

Specifically, I wanted to hit an instance of a DB connection.

As a result of various googles, I settled down as follows.

from bottle import route


class Sample:
    def __init__(self, dbcon):
        self.dbcon = dbcon
        route('/api/<arg>')(self.instance_method)

    def instance_method(self, arg):
        # <self.dbcon>Processing using

You hit the bottle's route method directly without using a decorator.

Recommended Posts

Hit a method of a class instance with the Python Bottle Web API
Hit the Etherpad-lite API with Python
Hit the web API in Python
[Python] A quick web application with Bottle!
Python: Prepare a serializer for the class instance:
Explosive speed with Python (Bottle)! Web API development
A python implementation of the Bayesian linear regression class
[python] [Gracenote Web API] A little customization of pygn
Call the API with python3.
Second half of the first day of studying Python Try hitting the Twitter API with Bottle
Don't take an instance of a Python exception class directly as an argument to the exception class!
A note about hitting the Facebook API with the Python SDK
A simple Python implementation of the k-nearest neighbor method (k-NN)
How to use the __call__ method in a Python class
Build a speed of light web API server with Falcon
Hit the Sesami API in Python
[Python] Hit the Google Translation API
[Python] Inherit a class with class variables
Create an instance of a predefined class from a string in Python
Save the result of the life game as a gif with python
Rubyist tried to make a simple API with Python + bottle + MySQL
The story of making a standard driver for db with python.
A function that measures the processing time of a method in python
Solve the Python knapsack problem with a branch and bound method
The idea of feeding the config file with a python file instead of yaml
I made a scaffolding tool for the Python web framework Bottle
How to send a request to the DMM (FANZA) API with python
Python beginners hit the unofficial API of Google Play Music to play music
The story of making a module that skips mail with python
Create a compatibility judgment program with the random module of python.
I want to clear up the question of the "__init__" method and the "self" argument of a Python class.
Create a Python function decorator with Class
Build a blockchain with Python ① Create a class
A class that hits the DMM API
Check the existence of the file with python
Search the maze with the python A * algorithm
Daemonize a Python web app with Supervisor
Download files on the web with Python
[python] Get a list of instance variables
[python] [meta] Is the type of python a type?
Run a Python web application with Docker
Let's make a web framework with Python! (1)
The story of blackjack A processing (python)
Let's make a web framework with Python! (2)
Get a capture of the entire web page in Selenium Python VBA
A story that visualizes the present of Qiita with Qiita API + Elasticsearch + Kibana
The story of making a university 100 yen breakfast LINE bot with Python
[AtCoder explanation] Control the A, B, C problems of ABC182 with Python!
Calculate the shortest route of a graph with Dijkstra's algorithm and Python
Get the number of searches with a regular expression. SeleniumBasic VBA Python
[AtCoder explanation] Control the A, B, C problems of ABC186 with Python!
[Introduction to Python] How to sort the contents of a list efficiently with list sort
I tried to get the authentication code of Qiita API with Python.
[AtCoder explanation] Control the A, B, C problems of ABC185 with Python!
Calculate the probability of being a squid coin with Bayes' theorem [python]
Receive a list of the results of parallel processing in Python with starmap
Get the number of articles accessed and likes with Qiita API + Python
[AtCoder explanation] Control the A, B, C problems of ABC187 with Python!
I tried to get the movie information of TMDb API with Python
Get a datetime instance at any time of the day in Python
[Introduction to Python] What is the method of repeating with the continue statement?