Skip to content

Commit e885baf

Browse files
authored
Add fastapitableau-example to Gallery (#77)
1 parent e03926e commit e885baf

File tree

8 files changed

+10139
-0
lines changed

8 files changed

+10139
-0
lines changed

.github/workflows/extensions.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ jobs:
3838
landing-page: extensions/landing-page/**
3939
stock-api-fastapi: extensions/stock-api-fastapi/**
4040
connectwidgets-example: extensions/connectwidgets-example/**
41+
fastapitableau-example: extensions/fastapitableau-example/**
4142
4243
# Runs for each extension that has changed from `simple-extension-changes`
4344
# Lints and packages in preparation for tests and and release.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Tableau Python Extension
2+
3+
## About this example
4+
5+
fastapitableau is a Python package that enables Python developers to build FastAPI APIs that function as Tableau Analytics Extensions. These extensions can be leveraged from Tableau workbooks to allow real time requests from Tableau to Python. This extension builds on top of Tableau's example Superstore dataset.
6+
7+
8+
## Learn more
9+
10+
* [Learn how to use this extension from Tableau](https://github.com/sol-eng/tableau-examples/tree/main/superstore)
11+
* [Documentation for fastapitableau](https://github.com/rstudio/fastapitableau)
12+
* [Configuring Posit Connect for use with Tableau](https://docs.posit.co/connect/admin/integrations/tableau/)
13+
14+
## Requirements
15+
16+
* Posit Connect license allows API publishing
17+
* Python version 3.9 or higher
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
from fastapitableau import FastAPITableau
2+
from joblib import load
3+
import pandas as pd
4+
from typing import List
5+
6+
# Load model
7+
model = load("model.joblib")
8+
9+
10+
# Define model_pipline
11+
def model_pipeline(dict):
12+
model_data = pd.DataFrame(dict)
13+
model_data["ship_diff"] = (
14+
model_data.days_to_ship_actual - model_data.days_to_ship_scheduled
15+
)
16+
pred_columns = ["ship_diff", "quantity", "sales", "discount"]
17+
return model.predict(model_data.loc[:, pred_columns]).tolist()
18+
19+
20+
# Define the extension
21+
app = FastAPITableau(
22+
title="Predicted Profit",
23+
description="A simple linear prediction of sales profit given new input data",
24+
version="0.1.0",
25+
)
26+
27+
28+
@app.post("/predict")
29+
async def predict(
30+
days_to_ship_actual: List[int],
31+
days_to_ship_scheduled: List[int],
32+
quantity: List[int],
33+
sales: List[float],
34+
discount: List[float],
35+
) -> List[float]:
36+
data = {
37+
"days_to_ship_actual": days_to_ship_actual,
38+
"days_to_ship_scheduled": days_to_ship_scheduled,
39+
"quantity": quantity,
40+
"sales": sales,
41+
"discount": discount,
42+
}
43+
return model_pipeline(data)

extensions/fastapitableau-example/clean-orders.csv

Lines changed: 9995 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
{
2+
"version": 1,
3+
"locale": "en_US.UTF-8",
4+
"metadata": {
5+
"appmode": "python-fastapi",
6+
"entrypoint": "app"
7+
},
8+
"extension": {
9+
"name": "fastapitableau-example",
10+
"title": "Tableau Python Extension",
11+
"description": "fastapitableau is a Python package that enables Python developers to build FastAPI APIs that function as Tableau Analytics Extensions. These extensions can be leveraged from Tableau workbooks to allow real time requests from Tableau to Python. This extension builds on top of Tableau's example Superstore dataset.",
12+
"homepage": "https://github.com/posit-dev/connect-extensions/tree/main/extensions/fastapitableau-example",
13+
"minimumConnectVersion": "2025.04.0",
14+
"version": "1.0.0"
15+
},
16+
"environment": {
17+
"python": {
18+
"requires": ">=3.9, <4"
19+
}
20+
},
21+
"python": {
22+
"version": "3.11.3",
23+
"package_manager": {
24+
"name": "pip",
25+
"version": "25.0.1",
26+
"package_file": "requirements.txt"
27+
}
28+
},
29+
"files": {
30+
"requirements.txt": {
31+
"checksum": "6a5a141024c9d4ec0a2313e25185e0b2"
32+
},
33+
"README.md": {
34+
"checksum": "6afae511be3a94f9f867276fc3786854"
35+
},
36+
"app.py": {
37+
"checksum": "9c935f4dc153c31b8991ddc8d18ffd7d"
38+
},
39+
"clean-orders.csv": {
40+
"checksum": "f576968ff5b8a02ccc975f45c079d80a"
41+
},
42+
"model.joblib": {
43+
"checksum": "ef047b888ba3b6225f0d55f57b63d466"
44+
},
45+
"train.py": {
46+
"checksum": "c77af4b24184b1e702424c24573444d7"
47+
}
48+
}
49+
}
974 Bytes
Binary file not shown.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
fastapitableau==1.3.0
2+
joblib==1.3.2
3+
pandas==2.2.1
4+
scikit-learn==1.4.1.post1
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#
2+
# Training for fastapitableau-example.
3+
# Reads clean-orders.csv.
4+
# Writes model.joblib.
5+
#
6+
7+
import pandas as pd
8+
from sklearn.linear_model import LinearRegression
9+
from joblib import dump
10+
11+
# Read in data
12+
data = pd.read_csv("clean-orders.csv")
13+
14+
# Define new features and training data
15+
data["ship_diff"] = data["days_to_ship_actual"] - data["days_to_ship_scheduled"]
16+
train_columns = [
17+
"ship_diff",
18+
"quantity",
19+
"sales",
20+
"discount",
21+
]
22+
train_data = data[train_columns]
23+
24+
# Fit linear regression model to predict profit
25+
reg = LinearRegression().fit(train_data, data.profit)
26+
reg.predict(train_data)
27+
data["predicted_profit"] = reg.predict(train_data)
28+
29+
# Write the model for use by the API.
30+
dump(reg, "model.joblib")

0 commit comments

Comments
 (0)