Skip the tutorial and deploy the example now
Deploy a Dash Application to Connect Cloud
This tutorial builds a Dash application and deploys it to Posit Connect Cloud. Code for the application is available on GitHub.
1. Create a new repo
Sign in to GitHub and create a new, public repository.
2. Start a new project
Start a new project in your preferred development environment. In VS Code, for example, select New Window from the File Menu and then click Start >> Clone Git Repository. Paste the link to your new repository.
3. Create a virtual environment
In the terminal, create a virtual environment named venv
and activate it. This will isolate the specific packages we need for the application.
-m venv venv
python /bin/activate source venv
4. Install required packages
dash
dash_bootstrap_components
plotly pandas
This example makes use of the above Python packages and they need to be installed in your virtual environment.
Create a requirements.txt
file, adding each package on a new line. This file helps install everything the application needs to run locally and will ultimately tell Connect Cloud what it needs to deploy the application.
Once you have saved requirements.txt
, run the following command in the terminal to install all the packages into your virtual environment.
-r requirements.txt pip install
5. Build the application
Create a new file named app.py
. Copy and paste the following code into the file.
# -*- coding: utf-8 -*-
import os
import dash
import dash_bootstrap_components as dbc
from dash import dash_table, dcc, html
from dash.dependencies import Input, Output
import pandas as pd
import plotly.express as px
= pd.Timestamp(2010, 1, 4, 0).date()
MIN_DATE = pd.Timestamp(2018, 11, 7, 0).date()
MAX_DATE
= dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
app
# Fetch prices from local CSV using pandas
= pd.read_csv(
prices __file__), "prices.csv"),
os.path.join(os.path.dirname(# index_col=0,
=True,
parse_dates="%Y-%m-%d",
date_format
)
"date"] = pd.to_datetime(prices["date"], format="%Y-%m-%d")
prices[= prices["ticker"].unique()
tickers
# Dataframe with top 5 volumes for each ticker
= (
max_vol "date"]).groupby("ticker")["volume"].nlargest(1).reset_index()
prices.set_index([
)= (
min_vol "date"]).groupby("ticker")["volume"].nsmallest(1).reset_index()
prices.set_index([
)= pd.concat([max_vol, min_vol])
extreme_vol = ["Stock", "Date", "Top and Lowest Volumes"]
extreme_vol.columns
# top nav bar
= dbc.Navbar(
nav =[
children
dbc.Row(
["Stock Tracker", className="ml-2")),
dbc.Col(dbc.NavbarBrand(
],="center",
align="g-0",
className
),
],="top",
sticky
)
# left side grouping of selction options
= dbc.Card(
form_card_group
[
dbc.Row(
["Choose a Stock Symbol", width=10),
dbc.Label(
dbc.Col(
dcc.Dropdown(id="stock-ticker-select",
=[
options
{"label": ticker,
"value": ticker,
}for ticker in tickers
],=True,
multi=[tickers[0]],
value
),
),
]
),
dbc.Row(
["Price", width="auto"),
dbc.Label(
dbc.Col(
dbc.RadioItems(id="stock-ticker-price",
=[
options
{"label": "Open",
"value": "open",
},
{"label": "High",
"value": "high",
},
{"label": "Low",
"value": "low",
},
{"label": "Close",
"value": "close",
},
],="close",
value
),=10,
width
),
]
),
html.Div(
[
dcc.Markdown("""
Selecting data in the **price** graph
will adjust the x-axis date range in the bottom **volume** graph.
"""
),id="selected-data"),
html.Pre(
],
),
],=True,
body
)
# sidebar
= {
SIDEBAR_STYLE "float": "left",
"top": "50px",
"left": 0,
"bottom": 0,
"width": "28rem",
"padding": "2rem 1rem",
}
= html.Div(
sidebar
form_card_group,=SIDEBAR_STYLE,
style
)
# price and volume graphs
= [
graphs
dbc.Alert("📊 Hover over the charts to highlight data points and show graph utilities. "
"All data is historical.",
="info",
color
),id="stock-price-graph", animate=True),
dcc.Graph(
dcc.Graph(id="stock-volume-graph",
=True,
animate
),
]
= dash_table.DataTable(
max_table_dash =max_vol.to_dict("records"),
data=True,
style_as_list_view=False,
fill_width={"float": "left"},
style_table={
style_cell"height": "auto",
# all three widths are needed
"minWidth": "180px",
"width": "180px",
"maxWidth": "180px",
"whiteSpace": "normal",
"padding-right": "30px",
"padding-left": "10px",
"text-align": "center",
},={"color": "black", "backgroundColor": "white"},
style_data=[
style_data_conditional
{"if": {"row_index": "odd"},
"backgroundColor": "rgb(220, 220, 220)",
}
],={
style_header"backgroundColor": "rgb(210, 210, 210)",
"color": "blue",
"fontWeight": "bold",
},
)
= dbc.Container(
body_container
[
html.Div(=[
children
dbc.Row(
[
dbc.Col(
sidebar,=4,
md
),
dbc.Col(
graphs,=8,
md
),
],
),
],="m-4",
className
),
html.Div(
[
dbc.Row(
[
dbc.Col(
[
dcc.Graph(id="scatter-plot",
={"float": "left"},
style
),
]
),
dbc.Col([max_table_dash]),
],
),
],
),
],=True,
fluid
)
# main app ui entry
= html.Div([nav, body_container])
app.layout
def filter_data_by_date(df, ticker, start_date, end_date):
"""Apply filter to the input dataframe
Args:
df: dateframe to filter
ticker: stock ticker symbol for filter criteria
start_date: min date threshold
end_date: max date threshold
Returns:
a filtered dataframe by ticker and date range
"""
if start_date is None:
= MIN_DATE
start_date
if end_date is None:
= MAX_DATE
end_date
= df[
filtered "ticker"] == ticker) & (df["date"] >= start_date) & (df["date"] <= end_date)
(df[
]return filtered
def volume_figure_layout(selected_tickers, xaxis_range=None):
"""Add layout specific to x-axis
Args:
selected_tickers: stock tickers for title
xaxis_range: `dict` with layout.xaxis.range config
Returns:
a layout dict
"""
= dict(xaxis={}, yaxis={})
layout "title"] = "Trading Volume (%s)" % (" & ").join(selected_tickers)
layout["yaxis"] = {"autorange": True}
layout["yaxis"]["title"] = "Volume"
layout["xaxis"]["title"] = "Trading Volume by Date"
layout[
if xaxis_range:
"xaxis"]["range"] = xaxis_range
layout["xaxis"]["autorange"] = True
layout[
return layout
@app.callback(
"stock-price-graph", "figure"),
Output(
["stock-ticker-select", "value"),
Input("stock-ticker-price", "value"),
Input(
],
)def update_price_figure(tickers, price):
"""Create a plot of stock prices
Args:
tickers: ticker symbols from the dropdown select
price: the radio button price selection
Returns:
a graph `figure` dict containing the specificed
price data points per stock
"""
return {
"data": [
{"x": [date for date in prices.loc[(prices.ticker == stock)]["date"]],
"y": [p for p in prices.loc[(prices.ticker == stock)][price]],
"type": "scatter",
"mode": "lines",
"name": stock,
}for stock in tickers
],"layout": {
"title": "Stock Price - (%s)" % " & ".join(tickers),
"xaxis": {"title": "Date"},
"yaxis": {"title": "Price"},
},
}
@app.callback(
"stock-volume-graph", "figure"),
Output(
["stock-ticker-select", "value"),
Input("stock-price-graph", "relayoutData"),
Input(
],
)def update_volume_figure(selected_tickers, relayoutData):
"""Create a plot of stock volume
Args:
selected_tickers: ticker symbols from the dropdown select
relayoutData: data emitted from a `selection` on the price graph
Returns:
a graph `figure` dict containing the specificed
volume data points per stock within the relayoutData
date range.
"""
= []
data = None
from_date = None
to_date
if relayoutData:
= relayoutData.get("xaxis.range[0]", None)
from_date = relayoutData.get("xaxis.range[1]", None)
to_date
if from_date and to_date:
= pd.Timestamp(from_date)
from_date = pd.Timestamp(to_date)
to_date
for stock in selected_tickers:
= filter_data_by_date(prices, stock, from_date, to_date)
filtered
data.append(
{"x": filtered["date"],
"y": filtered["volume"],
"type": "bar",
"name": stock,
}
)
= [from_date, to_date]
xaxis_range
return {
"data": data,
"layout": volume_figure_layout(selected_tickers, xaxis_range),
}
else:
= [
data
{"x": [item for item in prices[(prices.ticker == stock)]["date"]],
"y": [item for item in prices[(prices.ticker == stock)]["volume"]],
"type": "bar",
"name": stock,
}for stock in selected_tickers
]
# default dates
= [MIN_DATE, MAX_DATE]
xaxis_range
return {
"data": data,
"layout": volume_figure_layout(selected_tickers, xaxis_range),
}
return {"data": data, "layout": volume_figure_layout(selected_tickers)}
@app.callback(
"scatter-plot", "figure"),
Output(
["stock-ticker-select", "value"),
Input("stock-ticker-price", "value"),
Input(
],
)def update_scatter_plot(all_tickers, price):
= []
dfs if len(list(all_tickers)) < 2:
for stock in ["AAPL", "AMZN", "FB", "GOOG", "INTC", "MSFT"]:
= prices.loc[(prices["ticker"] == stock)]
temp = temp.loc[temp.date >= "2012-05-18"]
temp
dfs.append(temp)
= pd.concat(dfs, ignore_index=True)
final "daily ret"] = (final["close"] - final["open"]) * 100 / final["open"]
final[= final[["date", "ticker", "daily ret"]]
final
= pd.unique(temp["date"])
unique_dates = pd.DataFrame({"Date": unique_dates})
date_final
for stock in ["AAPL", "AMZN", "FB", "GOOG", "INTC", "MSFT"]:
= stock
col_name = final.loc[final["ticker"] == stock][
date_final[col_name] "daily ret"
].values= date_final.columns[1:]
ret_list = px.scatter_matrix(date_final, dimensions=ret_list)
fig =False)
fig.update_traces(diagonal_visible={"text": "Return Price Scatter Plot"})
fig.update_layout(titleelse:
for stock in all_tickers:
= prices.loc[(prices["ticker"] == stock)]
temp = temp.loc[temp.date >= "2012-05-18"]
temp
dfs.append(temp)
= pd.concat(dfs, ignore_index=True)
final "daily ret"] = (final["close"] - final["open"]) * 100 / final["open"]
final[= final[["date", "ticker", "daily ret"]]
final
= pd.unique(temp["date"])
unique_dates = pd.DataFrame({"Date": unique_dates})
date_final
for stock in all_tickers:
= stock
col_name = final.loc[final["ticker"] == stock][
date_final[col_name] "daily ret"
].values
= date_final.columns[1:]
ret_list = px.scatter_matrix(date_final, dimensions=ret_list)
fig =False)
fig.update_traces(diagonal_visible={"text": "Return Price Scatter Plot"})
fig.update_layout(title
return fig
if __name__ == "__main__":
app.run_server()
6. Preview the application
From the terminal, run the following command.
python app.py
Now that everything looks good and we’ve created a file to help reproduce your local environment, it is time to get the code on GitHub.
7. Push to GitHub
Before syncing your project with the new repository, create a .gitignore
file so that you don’t add the virtual environment to the repo.
touch .gitignore
Add the virtual environment folder to the .gitignore file.
/ venv
You are now ready to sync the project with your new repo. Run the following lines in the terminal, replacing https://github.com/account-name/repo-name.git with your repository’s link.
git init .-m "first commit"
git commit -M main
git branch //github.com/account-name/repo-name.git
git remote add origin https:-u origin main git push
Finally, refresh your GitHub repository page. You should see everything that was in your local directory with the exception of the virtual environment folder, which was excluded in the .gitignore
file.
8. Deploy to Posit Connect Cloud
Follow the steps below to deploy your project to Connect Cloud.
Sign in to Connect Cloud.
Click the Publish icon button on the top of your Profile page
Select Dash
Select the public repository that you created in this tutorial
Confirm the branch
Select app.py as the primary file
Click Publish
Publishing will display status updates during the deployment process. You will also find build logs streaming on the lower part of the screen.
Congratulations! You successfully deployed to Connect Cloud and are now able to share the link with others.
9. Republish the application
If you update the code to your application or the underlying data source, commit and push the changes to your GitHub repository.
Once the repository has the updated code, you can republish the application on Connect Cloud by going to your Content List and clicking the republish icon.