Skip to content

Commit

Permalink
fix file structure
Browse files Browse the repository at this point in the history
  • Loading branch information
tsengyushiang authored Mar 28, 2024
1 parent 87eebf9 commit a4f787e
Showing 1 changed file with 20 additions and 14 deletions.
34 changes: 20 additions & 14 deletions backend/python.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,31 +20,37 @@ def hello():
```python
import os
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from routes import api_bp
from extensions import db

app = Flask(__name__)
app.app_context().push() # avoid with app.app_context(). warning.

# register routes
app.register_blueprint(api_bp)
if __name__ == '__main__':
app = Flask(__name__)
app.app_context().push() # avoid with app.app_context(). warning.

# bind the database and ORM. The DB_URI is specified in the docker-compose file."
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('DB_URI')
db = SQLAlchemy(app)
app.register_blueprint(api_bp)

if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True)
# bind the database and ORM. The DB_URI is specified in the docker-compose file."
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('DB_URI')
db.init_app(app)
db.create_all()
app.run(host='0.0.0.0', debug=True)
```

## SQLAlchemy

### Initialize

### Define Schema
- Init instance in `extensions.py` to avoid circular import.

```python
from app import db
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
```

### Define Schema

```python
from extensions import db
from datetime import datetime

class User(db.Model):
Expand All @@ -61,7 +67,7 @@ class User(db.Model):

### APIs

- Initialize db `db.create_all()`
- Create tables by `db.create_all()`.

- Insert data

Expand Down

0 comments on commit a4f787e

Please sign in to comment.