Skip to content

Commit 73d75b3

Browse files
authored
Merge pull request sol-eng#15 from sol-eng/getting-started-flask
Getting started flask Examples
2 parents 6749942 + f85a7da commit 73d75b3

14 files changed

Lines changed: 262 additions & 1 deletion

File tree

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ rsconnect/
3939
.DS_Store
4040
__pycache__
4141
.ipynb_checkpoints
42-
*.html
42+
4343
rmarkdown-notebook/flights.csv
4444

4545
.venv
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
## Getting Started with Flask and RStudio Connect
2+
3+
This application structure and set-up follows the steps outlined in the links below.
4+
5+
Two routes are defined:
6+
7+
- `/` renders an HTML template
8+
- `/api/hello` returns a JSON object
9+
10+
#### Resources
11+
12+
- [RStudio Connect User Guide - Flask](https://docs.rstudio.com/connect/1.8.2/user/flask/)
13+
- [Getting Started with Flask and RStudio Connect](https://support.rstudio.com/hc/en-us/articles/360044700234)
14+
- [Deploying Flask Applications to RStudio Connect with Git and rsconnect-python](https://support.rstudio.com/hc/en-us/articles/360045224233)
15+
- [Using Templates and Static Assets with Flask Applications on RStudio Connect](https://support.rstudio.com/hc/en-us/articles/360045279313)

flask-getting-started-rsc/app.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from flask import Flask, render_template, jsonify
2+
3+
app = Flask(__name__)
4+
5+
6+
@app.route("/")
7+
def index():
8+
return render_template("index.html")
9+
10+
11+
@app.route("/api/hello", methods=["GET"])
12+
def hello():
13+
return jsonify({"message": "right back at ya!"})
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"
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": "4218d4579ae9bc8ee3c77acb556b8998"
19+
},
20+
"app.py": {
21+
"checksum": "9dcfbffbfa4f022525af8c47b9a0e976"
22+
},
23+
"static/api-snippet.png": {
24+
"checksum": "7d75b4ee6c3834edc79e114b8b371c21"
25+
},
26+
"static/style.css": {
27+
"checksum": "ee9656d5610fbc4ad05d1c1413618341"
28+
},
29+
"templates/index.html": {
30+
"checksum": "18c6159098b83ecc561a20b837236498"
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.2
9+
six==1.14.0
10+
SQLAlchemy==1.3.15
11+
Werkzeug==1.0.0
28.1 KB
Loading
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
.sans-serif {
2+
font-family: -apple-system, BlinkMacSystemFont, "avenir next", avenir,
3+
"helvetica neue", helvetica, ubuntu, roboto, noto, "segoe ui", arial,
4+
sans-serif;
5+
font-weight: 300;
6+
color: #333333;
7+
}
8+
9+
.ba {
10+
border-style: solid;
11+
border-width: 1px;
12+
}
13+
14+
.br2 {
15+
border-radius: 0.25rem;
16+
}
17+
18+
.b--black-30 {
19+
border-color: rgba(0, 0, 0, 0.3);
20+
}
21+
22+
.w-60 {
23+
width: 60%;
24+
}
25+
26+
.w-100 {
27+
width: 100%;
28+
}
29+
30+
.center {
31+
margin-right: auto;
32+
margin-left: auto;
33+
}
34+
35+
.tc {
36+
text-align: center;
37+
}
38+
39+
.pa {
40+
padding: 1rem;
41+
}
42+
43+
.mt5 {
44+
margin-top: 4rem;
45+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<html>
2+
<head>
3+
<link
4+
rel="stylesheet"
5+
type="text/css"
6+
href="{{ url_for('static', filename='style.css') }}"
7+
/>
8+
</head>
9+
<body class="sans-serif center w-60">
10+
<div class="mt5 ba pa br2 b--black-30 tc">
11+
<h2>🎉 Hello, World! This is Flask on RStudio Connect!</h2>
12+
</div>
13+
<div class="mt5 ba pa br2 b--black-30 tc">
14+
<h2>🚀Access the API endpoint</h2>
15+
<h3><a href="{{ url_for('hello') }}">{{ url_for('hello') }}</a></h3>
16+
17+
<img
18+
class="w-100"
19+
src="{{ url_for('static', filename='api-snippet.png') }}"
20+
/>
21+
</div>
22+
</body>
23+
</html>
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)

0 commit comments

Comments
 (0)