[Python] Web application from 0! Hands-on (3) -API implementation-

Overview

I had the opportunity to create a web application from scratch with Python, so as a summary! This article is about implementing the API.

I want this person to read

goal

The goal is to create a web app with CURD functionality using HTML, CSS, JavaScript, Python, and SQL.

Things necessary

1. Prepare the data in the database

First of all, I will try to connect to the database from the API and get the data. For that, let's prepare test data with pgAdmin. Start pgAdmin4, right-click TodoDatabase> Schemas> public> Tables> todo and select View / Edit Data> All Rows. 2020-07-31_18h33_10.png

title: Test created: 2020-07-31 18:39:00 is_deleted: false And press the Save Data Changes button.

2. Try connecting to the database from the API

Use the driver for PostgreSQL to connect the database. There are several of these, but I will use the most major (most installed) "psycopg2".

Install with the following command from the command prompt. pip install psycopg2

Then rewrite index.py as follows.

index.py


# -*- coding:utf-8 -*-

#Load an external package
from bottle import route, run
import psycopg2

#Get a connection to the database
def get_connection():
  return psycopg2.connect("host=localhost port=5432 dbname=TodoDatabase user=postgres password=postgres")

#Get Hello World
@route('/hello')
def hello():
  # Hello World!Returns the string
  return "Hello World!"

#Get Todo data
# @route('/todos')By, http://localhost:8080/You can access it with the URL todos
# with get_connection() as conn:Will automatically close the conn at the end of the scope
@route('/todos')
def getTodos():
  with get_connection() as conn:
    with conn.cursor() as cur:
      #Execute SQL
      cur.execute('SELECT * FROM todo')
      #Get 1 result of inquiry to database
      data = cur.fetchone()
      #Convert the data acquired from DB to a character string and return it
      return str(data)

#Web server execution configuration
#URL"http://[host]:[port]/[route]"Becomes the composition of
run(host='localhost', port=8080, debug=True)

Let's move it without thinking for the time being! Press F5 as you did in Hello World and select "Python File" to start the server. Then go to http: // localhost: 8080 / todos in your browser. image.png The data retrieved from the database is displayed!

Summary

We have implemented an API that displays database data!

next time···

If it is left as it is, it is difficult to understand how the data will be returned from the API ... right? (Training) Let's convert the data in the database to json format to make it easier to use!

Recommended Posts

[Python] Web application from 0! Hands-on (3) -API implementation-
[Python] Web application from 0! Hands-on (2) -Hello World-
[Python] Web application from 0! Hands-on (4) -Data molding-
[Python] Web application from 0! Hands-on (0) -Environmental construction-
[Python] Web application from 0! Hands-on (1) -Design, DB construction-
Python: Reading JSON data from web API
Data acquisition from analytics API with Google API Client for python Part 2 Web application
EXE Web API by Python
Web API with Python + Falcon
Web application with Python + Flask ② ③
Use e-Stat API from Python
Web application with Python + Flask ④
Web application development memo in python
Use kabu Station® API from Python
Hit the web API in Python
Use the Flickr API from Python
Get upcoming weather from python weather api
Run Ansible from Python using API
Use Google Analytics API from Python
Handle SOAP API from Python (Zeep)
Get US stock price from Python with Web API with Raspberry Pi
Collecting information from Twitter with Python (Twitter API)
Use Google Cloud Vision API from Python
I tried using UnityCloudBuild API from Python
[Python] A quick web application with Bottle!
[Python] Web application design for machine learning
Run a Python web application with Docker
API explanation to touch mastodon from python
Connect to coincheck's Websocket API from Python
Run a Python script from a C # GUI application
How to open a web browser from python
Procedure to use TeamGant's WEB API (using python)
sql from python
Steps to develop a web application in Python
[Python] Flow from web scraping to data analysis
MeCab from Python
Explosive speed with Python (Bottle)! Web API development
Get only articles from web pages in Python
Try accessing the YQL API directly from Python 3
Extract data from a web page with Python
2014 Web Application Framework Trends (PHP / Java / Ruby / Python / Perl)
Translator in Python from Visual Studio 2017 (Microsoft Translator Text API)
[Python] Get the text of the law from the e-GOV Law API
(Python) Try to develop a web application using Django
Push notifications from Python to Android using Google's API
Get schedule from Garoon SOAP API with Python + Zeep
Parse and visualize JSON (Web application ⑤ with Python + Flask)
Launch a Python web application with Nginx + Gunicorn with Docker
Web application made with Python3.4 + Django (Part.1 Environment construction)
Hit Watson's REST API from Python on IBM Bluemix
A little bit from Python using the Jenkins API
[python] [Gracenote Web API] A little customization of pygn
Use thingsspeak from python
Touch MySQL from Python 3
Django python web framework
RNN implementation in python
Operate Filemaker from Python
Use fluentd from python
ValueObject implementation in Python
Access bitcoind from python
Changes from Python 3.0 to Python 3.5