Skip to content

Commit 41d9ef0

Browse files
author
Xing Han Lu
authored
Merge pull request #631 from plotly/add-synapse-demo
Add Synapse Analytics Integration Demo (#minor) Former-commit-id: b1670a5
2 parents 05c0683 + 3b16636 commit 41d9ef0

17 files changed

Lines changed: 531 additions & 0 deletions

apps/dash-synapse-demo/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*__pycache__*
2+
*.vscode*

apps/dash-synapse-demo/Procfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
web: gunicorn app:server --workers 4 --preload

apps/dash-synapse-demo/README.md

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
# Dash Interest Rate Modeling (with Snowflake)
2+
3+
This app shows how to query loan data from a Snowflake Data Warehouse, and train and analyze a Ridge regression model using Dash.
4+
5+
![demo](assets/demo.gif)
6+
7+
## Setting up Synapse
8+
9+
This app uses an Azure Synapse Analytics database for querying the data used to train the model.
10+
11+
### Datasets
12+
13+
The data used was retrieved and modified from the [Lending Club Loan Data](https://www.kaggle.com/wendykan/lending-club-loan-data) from Kaggle. By using this dataset, you are agreeing to Kaggle's terms of use, as well as the original data license. You can download the modified files here:
14+
* `clean_loan.csv`: [Link](https://plotly-tutorials.s3-us-west-1.amazonaws.com/dash-sample-apps/snowflake-demos/clean_loan.csv)
15+
* `loan_desc.csv`: [Link](https://plotly-tutorials.s3-us-west-1.amazonaws.com/dash-sample-apps/snowflake-demos/loan_desc.csv)
16+
17+
### Installion ODBC driver
18+
19+
[This tutorial](https://docs.microsoft.com/en-us/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server?view=sql-server-ver15) shows how to install ODBC driver for SQL Server on Linux. You will need to install ODBC 17 to use this app - if you need another version, you will have to update the selected drivers by modifying the source code.
20+
21+
### SQL Setup from CSV file
22+
23+
Use the Azure Storage Explorer application to uploading `clean_loan.csv` to Azure Blob Storage. See [tutorial](https://docs.microsoft.com/en-us/azure/storage/blobs/storage-quickstart-blobs-storage-explorer).
24+
25+
26+
The following steps require Synapse Studio.
27+
28+
<!-- Connect to your Azure Blob Storage by selecting Data > "+" > Connect to external data > "Azure Blog Storage".
29+
30+
![](images/external_data_1.jpg)
31+
![](images/external_data_2.jpg) -->
32+
33+
34+
Create a database called loan:
35+
36+
![](images/create_sql_db.jpg)
37+
38+
Inside the SQL Editor, select "loan" for "Use database".
39+
40+
Create a new data source linking to the Azure Blob Storage containing the CSV file you previously uploaded. Execute the following (change the location if needed):
41+
```sql
42+
USE [loan];
43+
GO
44+
45+
CREATE EXTERNAL DATA SOURCE sampleapps
46+
WITH ( LOCATION = 'https://sampleappsdata.blob.core.windows.net/');
47+
```
48+
49+
Create a new format called "QuotedCsvWithHeaderFormat". Execute the following:
50+
51+
```sql
52+
USE [loan];
53+
GO
54+
55+
CREATE EXTERNAL FILE FORMAT CsvWithHeaderFormat
56+
WITH (
57+
FORMAT_TYPE = DELIMITEDTEXT,
58+
FORMAT_OPTIONS ( FIELD_TERMINATOR = ',', FIRST_ROW = 2 )
59+
);
60+
GO
61+
```
62+
63+
Finally, you can create your table:
64+
```sql
65+
USE [loan];
66+
GO
67+
68+
CREATE EXTERNAL TABLE cleanLoan
69+
(
70+
loan_amnt INT,
71+
funded_amnt INT,
72+
revol_bal INT,
73+
policy_code INT,
74+
funded_amnt_inv FLOAT,
75+
int_rate FLOAT,
76+
installment FLOAT,
77+
annual_inc FLOAT,
78+
dti FLOAT,
79+
delinq_2yrs FLOAT,
80+
inq_last_6mths FLOAT,
81+
open_acc FLOAT,
82+
pub_rec FLOAT,
83+
revol_util FLOAT,
84+
total_acc FLOAT,
85+
out_prncp FLOAT,
86+
out_prncp_inv FLOAT,
87+
total_pymnt FLOAT,
88+
total_pymnt_inv FLOAT,
89+
term VARCHAR (100) COLLATE Latin1_General_BIN2,
90+
grade VARCHAR (100) COLLATE Latin1_General_BIN2,
91+
emp_length VARCHAR (100) COLLATE Latin1_General_BIN2,
92+
home_ownership VARCHAR (100) COLLATE Latin1_General_BIN2,
93+
verification_status VARCHAR (100) COLLATE Latin1_General_BIN2,
94+
loan_status VARCHAR (100) COLLATE Latin1_General_BIN2,
95+
pymnt_plan VARCHAR (100) COLLATE Latin1_General_BIN2,
96+
purpose VARCHAR (100) COLLATE Latin1_General_BIN2,
97+
initial_list_status VARCHAR (100) COLLATE Latin1_General_BIN2,
98+
application_type VARCHAR (100) COLLATE Latin1_General_BIN2
99+
)
100+
WITH (
101+
LOCATION = 'dash-sample-apps-data/clean_loan.csv',
102+
DATA_SOURCE = sampleapps,
103+
FILE_FORMAT = CsvWithHeaderFormat
104+
);
105+
```
106+
107+
The columns can be generated with the following code (run after you downloaded `clean_loan.csv`):
108+
```python
109+
import pandas as pd
110+
import utils
111+
112+
df = pd.read_csv("clean_loan.csv", nrows=5)
113+
print(utils.get_column_strings(df))
114+
```
115+
116+
117+
### Using a different user
118+
119+
If you do not want to use your admin account, you can create one for the `loan` database limited to `SELECT`ing:
120+
```sql
121+
CREATE LOGIN developers WITH PASSWORD = '<your-password>';
122+
123+
USE [loan]
124+
GO;
125+
126+
CREATE USER developers FROM LOGIN developers;
127+
GRANT SELECT TO developers;
128+
```
129+
130+
Set the username to `developers` and the password to your new password.
131+
132+
Read more about [GRANT](https://docs.microsoft.com/en-us/azure/synapse-analytics/sql/sql-authentication?tabs=serverless) and [SQL authentication](https://docs.microsoft.com/en-us/azure/synapse-analytics/sql/sql-authentication?tabs=serverless).
133+
134+
135+
136+
## Setting up the app
137+
138+
First clone this repo:
139+
```
140+
git clone https://github.com/plotly/dash-sample-apps.git
141+
cd dash-sample-apps/apps/dash-interest-rate
142+
```
143+
144+
Create a conda env (or venv) and install the requirements:
145+
```
146+
conda create -n dash-interest-rate python=3.6.7
147+
conda activate dash-interest-rate
148+
pip install -r requirements.txt
149+
```
150+
151+
152+
## Start the app
153+
154+
Start a redis server:
155+
```
156+
redis-server --port 7777
157+
```
158+
159+
In a separate terminal window, you can now run the app:
160+
```
161+
python app.py
162+
```
163+
164+
and visit http://127.0.0.1:8050/.

apps/dash-synapse-demo/app.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"scripts": {
3+
"dokku": {
4+
"predeploy": "bash predeploy.sh"
5+
}
6+
}
7+
}

0 commit comments

Comments
 (0)