Skip to content

Commit 3c8be2c

Browse files
committed
[flask-sqlalchemy] getting started w/ flask + sqlalchemy app example
1 parent 19c9244 commit 3c8be2c

5 files changed

Lines changed: 105 additions & 0 deletions

File tree

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
## Using Flask-SQLAlchemy with Flask Applications on RStudio Connect
2+
3+
This application structure and set-up follows the steps outlined in
4+
[Using Flask-SQLAlchemy with Flask Applications on RStudio Connect](https://support.rstudio.com/hc/en-us/articles/360045926213):
5+
6+
- Creating a minimal application based on the Flask-SQLAlchemy quickstart guide
7+
- Define the database model to use
8+
- Initialize a SQLite database
9+
- Commit data to the database
10+
- Deploy the application to RStudio Connect with rsconnect-python
11+
12+
And, optionally:
13+
14+
- Switch to a PostgreSQL database server
15+
- Add `pyscopg2` dependency to the Python environment
16+
- Add the database server connection string as an environment variable in RStudio Connect
17+
- Redeploy the application
18+
19+
---
20+
21+
### Additional Resources
22+
23+
- [Getting Started with Flask and RStudio Connect](https://support.rstudio.com/hc/en-us/articles/360044700234)
24+
- [Deploying Flask Applications to RStudio Connect with Git and rsconnect-python](https://support.rstudio.com/hc/en-us/articles/360045224233)
25+
- [Using Templates and Static Assets with Flask Applications on RStudio Connect](https://support.rstudio.com/hc/en-us/articles/360045279313)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from flask import Flask, render_template
2+
from flask_sqlalchemy import SQLAlchemy
3+
import os
4+
5+
6+
app = Flask(__name__)
7+
basedir = os.path.abspath(os.path.dirname(__file__))
8+
9+
SQLALCHEMY_DATABASE_URI = os.environ.get("DATABASE_URI") or "sqlite:///" + os.path.join(
10+
basedir, "users.db"
11+
)
12+
13+
app.config["SQLALCHEMY_DATABASE_URI"] = SQLALCHEMY_DATABASE_URI
14+
15+
# initialize the extension
16+
db = SQLAlchemy(app)
17+
18+
19+
class User(db.Model):
20+
id = db.Column(db.Integer, primary_key=True)
21+
username = db.Column(db.String, unique=True, nullable=False)
22+
email = db.Column(db.String, unique=True, nullable=False)
23+
24+
25+
@app.route("/")
26+
def index():
27+
users = User.query.all()
28+
return render_template("index.html", users=users)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"version": 1,
3+
"metadata": {
4+
"appmode": "python-api",
5+
"entrypoint": "app:app"
6+
},
7+
"locale": "en_US.UTF-8",
8+
"python": {
9+
"version": "3.7.4",
10+
"package_manager": {
11+
"name": "pip",
12+
"version": "19.0.3",
13+
"package_file": "requirements.txt"
14+
}
15+
},
16+
"files": {
17+
"requirements.txt": {
18+
"checksum": "f00237682b99f60fa6d297c278e87fe5"
19+
},
20+
"app.py": {
21+
"checksum": "e4ccd14db159c5bcc7ea67cbfaff2cfb"
22+
},
23+
"static/style.css": {
24+
"checksum": "f49bd4d078ffd375f2deb76e0cdb5cf3"
25+
},
26+
"templates/index.html": {
27+
"checksum": "f3d3be99cf0a8467bdb2548b90f9076f"
28+
},
29+
"users.db": {
30+
"checksum": "7f391d911e8d45d2326a02f98fa45048"
31+
}
32+
}
33+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
click==7.1.1
2+
Flask==1.1.1
3+
Flask-SQLAlchemy==2.4.1
4+
itsdangerous==1.1.0
5+
Jinja2==2.11.1
6+
MarkupSafe==1.1.1
7+
psycopg2==2.8.4
8+
rsconnect-python==1.4.2.1
9+
six==1.14.0
10+
SQLAlchemy==1.3.15
11+
Werkzeug==1.0.0
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.ba {
2+
border-style: solid;
3+
border-width: 1px;
4+
}
5+
6+
.pa {
7+
padding: 1rem;
8+
}

0 commit comments

Comments
 (0)