Mysql on windows
- Go to
C:\Program Files\MySQL\MySQL Server 8.0\bin
and runcmd
- Now to connect to Mysql server write this command
mysql -u root -p
- Then Enter password
Create Flask project inside pycharm and JWT token
https://medium.com/@mushtaque87/flask-in-pycharm-community-edition-c0f68400d91e
- inside terminal run
pip install -r requirements.txt
- To create signature JWT write
python
inside terminal - Then write
import os
&os.urandom(12)
\xee[\xdc\x15\x88\xe8\xdf\xac\x85\xb4\x87\x16
http://localhost:5000/
to get jwt token run this MYSQL connector
SQLAlchemy already supports mysql and other dtabases driver.
Simply install mysql-connector
, mysql-connector-python
drivers
through pip install.
- Also, install
PyMysql
andCryptography
in the venv. - then in terminal run
python
and Create Table through: from app import db
db.create_all()
- Insert Data
from app import Product
p1 = Product('Siesta Key Water','2 Bedrooms \u00b7 2 Bathrooms' ,'https://media.c6.jpg',185)
db.session.add(p1)
db.session.commit()
- Query the Property
product = Product.query.all()
for p in product:
-
``print(p.propertyTitle)``
-
important reference:
- https://www.youtube.com/watch?v=hQl2wyJvK5k
- https://www.codestudyblog.com/cnb08/0818093912.html
- https://dev.to/blankgodd/connecting-to-a-mysql-database-with-sqlalchemy-lmc
GET request to get the properties from database
https://www.nintyzeros.com/2019/11/flask-mysql-crud-restful-api.html
Create a RESTful api using Flask and document it by generating API specification using openapi
Now Running below url
http://localhost:5000/property
Output is
{ "product": [ { "id": 1, "propertyBrand": "https://media.c6.jpg", "propertyDescription": "2 Bedrooms \u00b7 2 Bathrooms", "propertyPrice": 185, "propertyTitle": "Siesta Key Water" }, { "id": 2, "propertyBrand": "https://media.c7.jpg", "propertyDescription": "Sleeps 2 Studio 1 Bathroom", "propertyPrice": 222, "propertyTitle": "Ground Floor Direct" } ] }
Get request to get response from database and Openapi standard
http://localhost:5000/api/swagger.json
Output is
{ "info": { "title": "flask-api-swagger-doc", "version": "1.0.0" }, "openapi": "3.0.2", "paths": { "/property": { "get": { "description": "Get List of Properties", "responses": { "200": { "content": { "application/json": { "schema": { "$ref": "#/components/schemas/VrboPropertyResponseSchema" } } }, "description": "Return a Property list" } } } } } }
Get swagger UI run
http://localhost:5000/docs