-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodules snippet.txt
More file actions
104 lines (67 loc) · 3.39 KB
/
Copy pathmodules snippet.txt
File metadata and controls
104 lines (67 loc) · 3.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import itertools
import json
import re
import requests
from bs4 import BeautifulSoup
from collections import defaultdict
from unidecode import unidecode
import unicodedata
import typing, mypy
import colorama
import datetime
import functools
import nltk
import operator
import os
import sys
import time
from sqlalchemy import create_engine
engine = create_engine('sqlite:///database_name_here', echo=True')
Once you've created your database file, you need to create a table in it. First, create a model class that describes the table in the database.
All model classes should inherit from the DeclarativeMeta class that is returned by declarative_base():
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, Date
from datetime import datetime
Base = declarative_base()
class Table(Base):
__tablename__ = 'table_name'
id = Column(Integer, primary_key=True)
string_field = Column(String, default='default_value')
date_field = Column(Date, default=datetime.today())
def __repr__(self):
return self.string_field
Table is the name of the model class. It is used to access data from the table it describes. The name of the class can be anything.
__tablename__ specifies the table name in the database.
id is an integer column of the table; primary_key=True says that this column is the primary key.
string_field is a string column; default='default_value' says that the default value of this column is 'default_value'.
date_field is a column that stores the date. SQLAlchemy automatically converts the SQL date into a Python datetime object.
__repr__ method returns a string representation of the class object. In the ORM concept, each row in the table is an object of a class.
After we've described our table, it's time to create it in our database. All we need is to call the create_all() method and pass engine to it:
Base.metadata.create_all(engine)
Now we can access the database and store data in it. To access the database, we need to create a session:
from sqlalchemy.orm import sessionmaker
Session = sessionmaker(bind=engine)
session = Session()
To create a row in our table, you need to create an object of the model class and pass it to the add() method:
new_row = Table(string_field='This is a string field!',
date_field=datetime.strptime('01-24-2020', '%m-%d-%Y').date())
session.add(new_row)
session.commit()
To get all rows from the table, you can pass the model class to the query() method that selects all rows from the table represented by a model class:
rows = session.query(Table).all()
The all() method returns all rows from the table as a Python list. Each element of this list is an object of the model class. You can access the row fields by their names:
first_row = rows[0] # In case rows list is not empty
print(first_row.string_field) # Will print the value of the string_field
print(first_row.id) # Will print the id of the row
print(first_row) # Will print the string that was returned by the __repr__ method
--------
https://medium.com/analytics-vidhya/building-rest-apis-using-flask-restplus-sqlalchemy-marshmallow-cff76b202bfb
Following are the steps required to create a sample Flask-based API for an Item & Store management application:
1. Setup and Installation
2. Integrate Flask-Marshmallow
3. Integrate Flask-SQLAlchemy
4. Create Models
5. Create Schemas
6. Create Resources
7. Application Entry Point
8. Test the API