Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/GreatDt1/Flaskdt_App
https://github.com/GreatDt1/Flaskdt_App
Last synced: 8 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/GreatDt1/Flaskdt_App
- Owner: GreatDt1
- Created: 2021-07-29T13:13:41.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2022-08-14T19:50:53.000Z (over 2 years ago)
- Last Synced: 2024-08-13T07:08:51.833Z (4 months ago)
- Language: Python
- Size: 48.8 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
- jimsghstars - GreatDt1/Flaskdt_App - (Python)
README
# Welcome To Flask Dt
Here we are going to build a simple flask application to utilise flask-dt
First of first is to install the package
```bash
$ pip install flask-dt
```Below are notable versions of packages flask-dt requires
![notable versions](imgs/install_requires.PNG)Now onto the directories
In the current project directory, the only sub-directory needed is the templates directory which will hold your template files just like any normal flask application would require.
Make sure to have created itCreate it manually or you can use the command as below
```bash
$ mkdir templates
```Now onto the files
In the current project directory, a python file of any name
```bash
$ touch app.py
```In the templates directory, a html file of any name
This will display any of the tables in your db
```bash
$ touch display.html
```Python file
Import the required packages to your file as below
```python
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_dt import FlaskDt
```Create your flask application with basic configurations
Using sqlite as the rdbms is an arbitrary choice for simplicity```python
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
```Create your database SQLAlchemy instance
```python
db = SQLAlchemy(app)
```You can now create a FlaskDt instance as below
```pythondt = FlaskDt(db)
```
The FlaskDt instance needs the following arguments
- The SQLAlchemy database instance
- In this case it has the name dbCreate a simple table to see flask-dt in action!
Here we will use flask's ORM feature to define two tables```python
class Table1(db.Model):
id = db.Column(db.Integer, primary_key=True)
first_name = db.Column(db.String(20), nullable=False)class Table2(db.Model):
id = db.Column(db.Integer, primary_key=True)
first_name = db.Column(db.String(20), nullable=False)
last_name = db.Column(db.String(20), nullable=False)
age = db.Column(db.Integer, nullable=False)
```Create a simple route for the home page
```python
@app.route('/home')
@app.route('/')
def home_page():
return "Hello"
```Create a route to display the tables
```python
@app.route('/tables/')
# @login_required
@dt.display_table
def table_page(**kwargs):
tablename = kwargs.get('tablename', None)
table_class = kwargs.get('table_class', None)
columns = kwargs.get('columns', None)
records = kwargs.get('records', None)if table_class:
return render_template('display.html', columns=columns, records=records, tablename=tablename)else:
return f'No such table {tablename} in the database. Try creating the table'
```
This serves to handle the routes to any of the tables in your db- Note the @dt.display_table decorator for the route
- Note the string variable named **tablename** in the route. The decorator will be looking for the given variable name on execution
- Note the commented @login.required decorator indicating you can chain decorators with dt tooA simple prompt will appear for an invalid table name
We will create the tables in the db shortlyFinally running the app
```python
if __name__ == "__main__":
app.run(debug=True)
```The final python file is in the repository
## creating the tables
We summon the python shell in the terminal as follows
```bash
$ python
```We import the db instance then create the tables
```bash
>>>from app import db
>>>db.create_all()
>>>
```We can add sample data to the tables created
```bash
>>>from app import Table1, Table2
>>>record1 = Table1(first_name="John")
>>>db.session.add(record1)
>>>db.session.commit()
>>>record2 = Table2(first_name="Mary", last_name="Jane", age=23)
>>>db.session.add(record2)
>>>db.session.commit()
>>>exit()
```We now run the python file named app in the terminal
```bash
$ python app.py
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
```The home page should look like this
![home_page](imgs/home_page.PNG)Now we head to the route using the following format
- /route/tablenameThe first table should look like this
![first_table_route](imgs/table_1.PNG)The second table should look like this
![first_table_route](imgs/table_2.PNG)## Something to Note
The dt decorator passes the following variables to your function
- records
- this is a list of all records in the db where each record is a dict
- columns
- this is a list of all columns in the db
- tablename
- this is, ofcourse, the name of your table
- table_class
- this is the SQLAlchemy instance of your tableWith that a simple for loop can be used to display all the records in the table as below
```html
{% for column in columns %}
{{column}}
{% endfor %}
{% for record in records %}
{% for column in columns %}
{{record.column}}
{% endfor %}
{% endfor %}
```It would be amazing to know all your experiences with flask-dt. Projects accomplished, ideas regarding the package, improvement suggestions, basically anything and everything.
Enjoy!