GithubHelp home page GithubHelp logo

efsane58911 / flask-database Goto Github PK

View Code? Open in Web Editor NEW

This project forked from matthewscholefield/flask-database

0.0 0.0 0.0 9 KB

Simple Flask database integration

License: MIT License

Python 100.00%

flask-database's Introduction

Flask Database

Simple Flask database integration

This is a library to make it simple to run sql queries using flask.

When running database queries in Python with Flask, you either need to create a new connection for each request (quite slow) or manage some pool of connections (complicated). DBUtils already solves this problem. This package hooks DBUtils into flask's session so that you can access the database in a simple manner.

Usage

import pymysql  # Can be any database type
import sys
from flask_database import Database
from flask import Flask, request

app = Flask(__name__)
db = Database(
    app,
    pymysql,
    host='127.0.0.1',
    port=3306,
    user='my_user',
    password='123',
    database='my_database'
)

@app.route('/revenue', methods=['GET'])
def get_revenue():
    purchases = db.fetch_all('SELECT price, quantity FROM Purchases')
    revenue = sum(price * quantity for price, quantity in purchases)
    return 'Revenue: ' + str(revenue)

@app.route('/purchase', methods=['POST'])
def post_purchase():
    price = float(request.form['price'])
    quantity = int(request.form['quantity'])
    db.run('INSERT INTO Purchases(price, quantity) VALUES (%s, %s)', (price, quantity))
    return 'Success'


def init_tables():
    db.detach()  # Allows use of database outside Flask request
    db.run('CREATE TABLE Purchases(price DOUBLE NOT NULL, quantity INTEGER NOT NULL)')


if sys.argv[1] == 'init':
    init_tables()
else:
    app.run()

Now, we can interact with our service as you would expect:

curl http://127.0.0.1:5000/revenue
# Revenue: 0

curl -X POST -F 'price=1.25' -F 'quantity=9' http://127.0.0.1:5000/purchase
# Success

curl http://127.0.0.1:5000/revenue
# Revenue: 11.25

Note: to replicate this example you'll have MySQL runnning with the provided database and user set up. Example commands:

sudo apt-get install mysql-server
sudo mysql -u root # If you already set a password use 'mysql -u root -p'
CREATE DATABASE my_database;
CREATE USER my_user@localhost IDENTIFIED BY '123';
GRANT ALL PRIVILEGES ON my_database. * TO my_user@localhost;
python3 example.py init  # Setup tables
python3 example.py  # Run server

Installation

Install via PyPI:

pip3 install --user flask-database

flask-database's People

Contributors

matthewscholefield avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.