Skip to main content

Posts

Showing posts from September, 2018

Python - Good API File Design for RESTful API in Flask

🐍 Let us go through a good design to writing APIs. Resources > entity.py #inside resources create a python file for the entity from flask_restful import Resource from models.store import StoreModel class Store(Resource):     def get(self, name):         #get from models         store = storeModel.find_by_name(name)         #if got         if store:             return store.json()         #else         return {'message': 'Store not found'}, 404     def post(self, name):         #check if already exists by get request with the name         if storeModel.find_by_name(name):             return {'message': 'A Store with name {} already there in database. Use another name.'.format(name)}, 400         #if not the...

Python - SQLAlchemy for Shorter CRUD Actions on Database

🐍 I tried SQLAlchemy, as well as CRUD without SQLAlchemy. With SQLAlchemy, a 7 to 10 line code is converted to a 2 line code. Saving to database is: db.session.add(self) db.session.commit Similarly delete is: db.session.delete(self) db.session.commit() Fetching data is a single line code: ItemModel.query.all() I wonder why to have a long method of saving in the first place and then use a library to shorten it. Do you have an answer?

Python - Setup a New Project and use Virtual Environment

🐍 How to set up a new project and use Virtual Environment? Run the following on Terminal to set up a new project in a Virtual Environment. mkdir Test_Project_2 cd Test_Project_2 mkdir code virtualenv venv source venv/bin/activate pip install Flask pip install Flask-JWT pip install Flask-RESTful pip install Flask-SQLAlchemy Answer to: How to set up and use a virtual python environment? What is the best way to install python packages

Python - How to place app.run() function

🐍Always place app.run() in the if statement as displayed below in your app.py file: if __name__ == '__main__':     app.run(port=5000, debug=True) This helps in case you import app.py from another file. In that case, you don't want to run the app.run() function. With this 'if' statement, it will only run if app.py is directly run.

SQLite + Python - Create Table, Post Values, Get Values

πŸ•Š️ This simple program helps to use SQLite as a database and carry out operations such as create table and CRUD values into it. #use sqlite import sqlite3 #establish connection connection = sqlite3.connect('data.db') cursor = connection.cursor() #create table create_table = "CREATE TABLE users (id int, username text, password text)" cursor.execute(create_table) #add a record to table user = (1, 'user1', 'pass1') insert_query = "INSERT INTO users VALUES (?, ?, ?)" cursor.execute(insert_query, user) #add multiple records to table users = [     (2, 'user2', 'pass2'),     (3, 'user3', 'pass3') ] cursor.executemany(insert_query, users) #retrieve all records from a table select_query = "SELECT * from users" for row in cursor.execute(select_query):     print(row) #commit and close connection connection.commit() connection.close()

Python - Simple Flask App for Get API

🐍 Create a file named app.py with the following code and run it on terminal. from flask import Flask from flask_restful import Resource, Api app = Flask(__name__) api = Api(app) class Student(Resource):     def get(self, name):         return {'student': name} api.add_resource(Student, '/student/<string:name>') app.run(port=5000) This application imports Flask, Resource, and API. Then creates an app and api objects. Then creates a class. Then calls a function using api object to enable GET request. Then it calls run function using app object to host it on port=5000

Python - Create and Activate Virtual Environment in Python

🐍 Here is how to use Virtual Environment in Python Install Virtual Environment pip install virtualenv Create a Virtual Environment and name it "venv" in python 2.7 virtualenv venv --python=python2.7 Activate venv source venv/bin/activate Install Flask-RESTful and Flask in venv pip install flask pip install flask-restful Deactivate venv deactivate