10-18-2022, 05:27 AM
Plotly Dash Bootstrap Card Components
Rate this post
Welcome to the bonus content of “The Book of Dash”.![[Image: 1f917.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f917.png)
Here you will find additional examples of Plotly Dash components, layouts and style. To learn more about making dashboards with Plotly Dash, and how to buy your copy of “The Book of Dash”, please see the reference section at the bottom of this article.
As you read the article, feel free to run the explainer video on the Card components from one of our coauthors’ “Charming Data” YT channel:
![[Image: lyteCache.php?origThumbUrl=https%3A%2F%2...efault.jpg]](https://blog.finxter.com/wp-content/plugins/wp-youtube-lyte/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2FaEz1-72PKwc%2Fhqdefault.jpg)
This article will focus on the components from the Dash Boostrap Component library. Using cards is a great way to create eye-catching content. We’ll show you how to make the card content interactive with callbacks, but first we’ll focus on the style and layout.
Plotly Dash App with a Bootstrap Card
We’ll start with the basics – a minimal Dash app to display a single card without any additional styling. Be sure to check out the complete reference for using Dash Bootstrap cards.
Next, we’ll show how to jazz it up to make it look better — and more importantly — so it conveys key information at a glance.
![[Image: image-143.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-143.png)
Styling a Dash Bootstrap Card
An easy way to style content is by using Boostrap utility classes. See all the utility classes at the Dash Bootstrap Cheatsheet app. This handy cheatsheet is made by a co-author of “The Book of Dash”.
In this card, we center the text and change the color with “” and ““. The Bootstrap themes have named colors and “success” is a shade of green.
Recommended Resource: For more information about styling your app with a Boostrap theme, see Dash Bootstrap Theme Explorer
![[Image: image-142.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-142.png)
Feel free to watch Adam’s explainer video on Bootstrap and styling your app if you need to get up to speed!![[Image: 1f447.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f447.png)
![[Image: lyteCache.php?origThumbUrl=https%3A%2F%2...efault.jpg]](https://blog.finxter.com/wp-content/plugins/wp-youtube-lyte/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2FvqVwpL4bGKY%2Fhqdefault.jpg)
Dash Bootstrap Card with Icons
You can add Bootstrap and/or Font Awesome icons to your Dash Bootstrap components. In this example, we will add the bank icon as well as change the background color using the Bootstrap utility class.
![[Image: image-141.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-141.png)
To learn more, see the Icons section of the documentation. You can also find more information about adding icons to dash components in the buttons article.
Recommended Tutorial: Plotly Dash Button Component – A Simple Illustrated Guide
Dash Bootstrap Cards Side-by-Side
In business intelligence dashboards, it’s common to highlight KPIs or Key Performance Indicators in a group of cards. You can find many examples in the Plotly App Gallery:
![[Image: image-154-1024x729.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-154-1024x729.png)
This app places three KPI cards side-by-side. We use the and components to create this responsive card layout. When you run this app, try changing the width of the browser window to see how the cards expand to fill the row based on the screen size.
This app also demonstrates the usage of Bootstrap utility classes to add and style a border. Here we add a border on the left and change the color to highlight the results. Another trick is to use the “” class to keep the icon and the text together on the same line when the cards shrink to accommodate small screen sizes.
![[Image: image-144.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-144.png)
Creating Dash Bootstrap Cards in a Loop
In the previous example, notice that a lot of the code for creating the card is the same. To reduce the amount of repetitive code, let’s create cards in a function.
In this app, we introduce the component and the class to style the card. We’ll show you how to add more style later in the app that displays crypto prices.
![[Image: image-145.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-145.png)
Dash Bootstrap Card with an Image
This card uses the component. This is a great format for the “who’s who” section of your app. It works well for displaying information about products too.
![[Image: image-146.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-146.png)
Dash Bootstrap Card with an Image and a Link
This app has a card with the component.
When you run this app, try clicking on either the logo or the title. You will see that both are links to the Plotly site displaying the current job openings.
We do this by including both the component with the Plotly logo and the with the title in the component.
![[Image: image-147.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-147.png)
Dash Bootstrap Card with a Background Image
This app puts the image in the background and uses the component to place content on top of the image.
We also uses to link to other sites for more information. See the buttons article for more information. Be sure to run the app and check out the links. The Webb Telescope app is pretty cool!
![[Image: image-148.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-148.png)
Recommended Tutorial: Before After Image in Plotly Dash
See this Plotly Dash app live: https://jwt.pythonanywhere.com/
![[Image: image-149.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-149.png)
Plotly Dash App with Live Updates
This app shows live updates of crypto prices. We use a component to fetch the data from CoinGecko every 6 seconds.
The CoinGecko API is easy to use because you don’t need an API key, and it’s free if you keep the number of updates within the free tier limits. We pull the current price, 24 hour price change, and the coin logo from the data feed and display the data in a nicely styled card.
In this app we introduce callbacks to update the data, and show how to get the data from CoinGecko. All the other styling has been covered in previous examples.
Note that in this app, the color of the text and the up and down arrows are updated dynamically based on the data in the function.
![[Image: image-150-1024x268.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-150-1024x268.png)
Plotly Dash App with a Sidebar
A common layout for Dash apps is to put inputs in a sidebar, and the output in the main section of the page. We can place both the sidebar and the output in Dash Boostrap Card components.
See the app and the code live at the Dash Example Index
![[Image: image-151-1024x442.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-151-1024x442.png)
Plotly Dash Example Index
See more examples of interactive apps in the Dash Example Index
![[Image: image-152-1024x767.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-152-1024x767.png)
Reference
Order Your Copy of “The Book of Dash” Today!
![[Image: 185497519-733bdfc3-5731-4419-9a68-44c1cad04a78.png]](https://user-images.githubusercontent.com/72614349/185497519-733bdfc3-5731-4419-9a68-44c1cad04a78.png)
https://github.com/DashBookProject/Plotl...-with-dash
The Book of Dash Authors
Feel free to learn more about the book’s coauthors here:
Ann Marie Ward:
Adam Schroeder:
Chris Mayer:
Rate this post
Welcome to the bonus content of “The Book of Dash”.
![[Image: 1f917.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f917.png)
Here you will find additional examples of Plotly Dash components, layouts and style. To learn more about making dashboards with Plotly Dash, and how to buy your copy of “The Book of Dash”, please see the reference section at the bottom of this article.As you read the article, feel free to run the explainer video on the Card components from one of our coauthors’ “Charming Data” YT channel:
This article will focus on the
Code:
CardPlotly Dash App with a Bootstrap Card
We’ll start with the basics – a minimal Dash app to display a single card without any additional styling. Be sure to check out the complete reference for using Dash Bootstrap cards.
Next, we’ll show how to jazz it up to make it look better — and more importantly — so it conveys key information at a glance.
![[Image: image-143.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-143.png)
Code:
from dash import Dash, html
import dash_bootstrap_components as dbc app = Dash(__name__, external_stylesheets=[dbc.themes.SPACELAB, dbc.icons.BOOTSTRAP]) card = dbc.Card( dbc.CardBody( [ html.H1("Sales"), html.H3("$104.2M") ], ),
) app.layout=dbc.Container(card) if __name__ == "__main__": app.run_server(debug=True)An easy way to style content is by using Boostrap utility classes. See all the utility classes at the Dash Bootstrap Cheatsheet app. This handy cheatsheet is made by a co-author of “The Book of Dash”.
In this card, we center the text and change the color with “
Code:
text-centerCode:
text-success
Recommended Resource: For more information about styling your app with a Boostrap theme, see Dash Bootstrap Theme Explorer![[Image: image-142.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-142.png)
Code:
card = dbc.Card( dbc.CardBody( [ html.H1("Sales"), html.H3("$104.2M", className="text-success") ], ), className="text-center"
)Feel free to watch Adam’s explainer video on Bootstrap and styling your app if you need to get up to speed!
![[Image: 1f447.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f447.png)
Dash Bootstrap Card with Icons
You can add Bootstrap and/or Font Awesome icons to your Dash Bootstrap components. In this example, we will add the bank icon as well as change the background color using the Bootstrap utility class
Code:
bg-primary![[Image: image-141.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-141.png)
Code:
card = dbc.Card( dbc.CardBody( [ html.H1([html.I(className="bi bi-bank me-2"), "Profit"]), html.H3("$8.3M"), html.H4(html.I("10.3% vs LY", className="bi bi-caret-up-fill text-success")), ], ), className="text-center m-4 bg-primary text-white",
)To learn more, see the Icons section of the
Code:
dash-bootstrap-components
Recommended Tutorial: Plotly Dash Button Component – A Simple Illustrated GuideDash Bootstrap Cards Side-by-Side
In business intelligence dashboards, it’s common to highlight KPIs or Key Performance Indicators in a group of cards. You can find many examples in the Plotly App Gallery:
![[Image: image-154-1024x729.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-154-1024x729.png)
This app places three KPI cards side-by-side. We use the
Code:
dbc.RowCode:
dbc.ColThis app also demonstrates the usage of Bootstrap
Code:
borderCode:
text-nowrap![[Image: image-144.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-144.png)
Code:
from dash import Dash, html
import dash_bootstrap_components as dbc app = Dash(__name__, external_stylesheets=[dbc.themes.SPACELAB, dbc.icons.BOOTSTRAP]) card_sales = dbc.Card( dbc.CardBody( [ html.H1([html.I(className="bi bi-currency-dollar me-2"), "Sales"], className="text-nowrap"), html.H3("$106.7M"), html.Div( [html.I("5.8%", className="bi bi-caret-up-fill text-success"), " vs LY",] ), ], className="border-start border-success border-5" ), className="text-center m-4"
) card_profit = dbc.Card( dbc.CardBody( [ html.H1([html.I(className="bi bi-bank me-2"), "Profit"], className="text-nowrap"), html.H3("$8.3M",), html.Div( [ html.I("12.3%", className="bi bi-caret-down-fill text-danger"), " vs LY", ] ), ], className="border-start border-danger border-5" ), className="text-center m-4",
) card_orders = dbc.Card( dbc.CardBody( [ html.H1([html.I(className="bi bi-cart me-2"), "Orders"], className="text-nowrap"), html.H3("91.4K"), html.Div( [ html.I("10.3%", className="bi bi-caret-up-fill text-success"), " vs LY", ] ), ], className="border-start border-success border-5" ), className="text-center m-4",
) app.layout = dbc.Container( dbc.Row( [dbc.Col(card_sales), dbc.Col(card_profit), dbc.Col(card_orders)], ), fluid=True,
) if __name__ == "__main__": app.run_server(debug=True)In the previous example, notice that a lot of the code for creating the card is the same. To reduce the amount of repetitive code, let’s create cards in a function.
In this app, we introduce the
Code:
dbc.CardHeaderCode:
"shadow"![[Image: image-145.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-145.png)
Code:
from dash import Dash, html
import dash_bootstrap_components as dbc app = Dash(__name__, external_stylesheets=[dbc.themes.SPACELAB]) summary = {"Sales": "$100K", "Profit": "$5K", "Orders": "6K", "Customers": "300"} def make_card(title, amount): return dbc.Card( [ dbc.CardHeader(html.H2(title)), dbc.CardBody(html.H3(amount, id=title)), ], className="text-center shadow", ) app.layout = dbc.Container( dbc.Row([dbc.Col(make_card(k, v)) for k, v in summary.items()], className="my-4"), fluid=True,
) if __name__ == "__main__": app.run_server(debug=True)This card uses the
Code:
dbc.CardImage![[Image: image-146.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-146.png)
Code:
from dash import Dash, html
import dash_bootstrap_components as dbc app = Dash(__name__, external_stylesheets=[dbc.themes.SPACELAB]) count = "https://user-images.githubusercontent.com/72614349/194616425-107a62f9-06b3-4b84-ac89-2c42e04c00ac.png" card = dbc.Card([ dbc.CardImg(src=count, top=True), dbc.CardBody( [ html.H3("Count von Count", className="text-primary"), html.Div("Chief Financial Officer"), html.Div("Sesame Street, Inc.", className="small"), ] )], className="shadow my-2", style={"maxWidth": 350},
) app.layout=dbc.Container(card) if __name__ == "__main__": app.run_server(debug=True)This app has a card with the
Code:
dbc.CardLinkWhen you run this app, try clicking on either the logo or the title. You will see that both are links to the Plotly site displaying the current job openings.
We do this by including both the
Code:
html.ImgCode:
html.SpanCode:
dbc.CardLink![[Image: image-147.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-147.png)
Code:
from dash import Dash, html
import dash_bootstrap_components as dbc app = Dash(__name__, external_stylesheets=[dbc.themes.SPACELAB]) plotly_logo_dark = "https://user-images.githubusercontent.com/72614349/182967824-c73218d8-acbf-4aab-b1ad-7eb35669b781.png" card = dbc.Card( dbc.CardBody( [ dbc.CardLink( [ html.Img(src=plotly_logo_dark, height=65), html.Span("Plotly Job Openings", className="ms-2") ], className="text-decoration-none h2", href="https://plotly.com/careers/" ), html.Hr(), html.Div("Engineering", className="h3"), html.Div("Intermediate Backend Engineer", className="text-danger"), html.Div("Remote, Canada", className="small"), ] ), className="shadow my-2", style={"maxWidth": 450},
) app.layout=dbc.Container(card) if __name__ == "__main__": app.run_server(debug=True)This app puts the image in the background and uses the
Code:
dbc.CardImgOverlayWe also use
Code:
dbc.Button![[Image: image-148.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-148.png)
Recommended Tutorial: Before After Image in Plotly DashCode:
from dash import Dash, html
import dash_bootstrap_components as dbc app = Dash(__name__, external_stylesheets=[dbc.themes.SPACELAB, dbc.icons.BOOTSTRAP]) webb_deep_field = "https://user-images.githubusercontent.com/72614349/192781103-2ca62422-2204-41ab-9480-a730fc4e28d7.png"
card = dbc.Card( [ dbc.CardImg(src=webb_deep_field), dbc.CardImgOverlay([ html.H2("James Webb Space Telescope"), html.H3("First Images"), html.P( "Learn how to make an app to compare before and after images of Hubble vs Webb with ~40 lines of Python", style={"marginTop":175}, className="small", ), dbc.Button("See the App", href="https://jwt.pythonanywhere.com/"), dbc.Button( [html.I(className="bi bi-github me-2"), "source code"], className="ms-2 text-white", href="https://github.com/AnnMarieW/webb-compare", ) ]) ], style={"maxWidth": 500}, className="my-4 text-center text-white"
) app.layout=dbc.Container(card) if __name__ == "__main__": app.run_server(debug=True)See this Plotly Dash app live: https://jwt.pythonanywhere.com/
![[Image: image-149.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-149.png)
Plotly Dash App with Live Updates
This app shows live updates of crypto prices. We use a
Code:
dcc.IntervalThe CoinGecko API is easy to use because you don’t need an API key, and it’s free if you keep the number of updates within the free tier limits. We pull the current price, 24 hour price change, and the coin logo from the data feed and display the data in a nicely styled card.
In this app we introduce callbacks to update the data, and show how to get the data from CoinGecko. All the other styling has been covered in previous examples.
Note that in this app, the color of the text and the up and down arrows are updated dynamically based on the data in the
Code:
make_card![[Image: image-150-1024x268.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-150-1024x268.png)
Code:
import dash
from dash import Dash, dcc, html, Input, Output
import dash_bootstrap_components as dbc
import requests app = Dash(__name__, external_stylesheets=[dbc.themes.SUPERHERO, dbc.icons.BOOTSTRAP]) coins = ["bitcoin", "ethereum", "binancecoin", "ripple"]
interval = 6000 # update frequency - adjust to keep within free tier
api_url = "https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd" def get_data(): try: response = requests.get(api_url, timeout=1) return response.json() except requests.exceptions.RequestException as e: print(e) def make_card(coin): change = coin["price_change_percentage_24h"] price = coin["current_price"] color = "danger" if change < 0 else "success" icon = "bi bi-arrow-down" if change < 0 else "bi bi-arrow-up" return dbc.Card( html.Div( [ html.H4( [ html.Img(src=coin["image"], height=35, className="me-1"), coin["name"], ] ), html.H4(f"${price:,}"), html.H5( [f"{round(change, 2)}%", html.I(className=icon), " 24hr"], className=f"text-{color}", ), ], className=f"border-{color} border-start border-5", ), className="text-center text-nowrap my-2 p-2", ) mention = html.A( "Data from CoinGecko", href="https://www.coingecko.com/en/api", className="small"
)
interval = dcc.Interval(interval=interval)
cards = html.Div()
app.layout = dbc.Container([interval, cards, mention], className="my-5") @app.callback(Output(cards, "children"), Input(interval, "n_intervals"))
def update_cards(_): coin_data = get_data() if coin_data is None or type(coin_data) is dict: return dash.no_update # make a list of cards with updated prices coin_cards = [] updated = None for coin in coin_data: if coin["id"] in coins: updated = coin.get("last_updated") coin_cards.append(make_card(coin)) # make the card layout card_layout = [ dbc.Row([dbc.Col(card, md=3) for card in coin_cards]), dbc.Row(dbc.Col(f"Last Updated {updated}")), ] return card_layout if __name__ == "__main__": app.run_server(debug=True)A common layout for Dash apps is to put inputs in a sidebar, and the output in the main section of the page. We can place both the sidebar and the output in Dash Boostrap Card components.
See the app and the code live at the Dash Example Index
![[Image: image-151-1024x442.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-151-1024x442.png)
Plotly Dash Example Index
See more examples of interactive apps in the Dash Example Index
![[Image: image-152-1024x767.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-152-1024x767.png)
Reference
Order Your Copy of “The Book of Dash” Today!
![[Image: 185497519-733bdfc3-5731-4419-9a68-44c1cad04a78.png]](https://user-images.githubusercontent.com/72614349/185497519-733bdfc3-5731-4419-9a68-44c1cad04a78.png)
https://github.com/DashBookProject/Plotl...-with-dash
- Dash documentation – tutorial. Getting Started with Dash
- Dash Example Index. Sample apps with sliders
- Dash Bootstrap Components documentation – Themes
- Dash Bootstrap Components documentation – Code:
dbc.Card
- Dash Bootstrap Theme Explorer. A guide for styling Plotly Dash apps with a Bootstrap theme
The Book of Dash Authors
Feel free to learn more about the book’s coauthors here:
Ann Marie Ward:
GitHub: https://github.com/AnnMarieW
Dash Forum: https://community.plotly.com/u/annmariew/summary
Adam Schroeder:
YouTube CharmingData: https://www.youtube.com/c/CharmingData
Chris Mayer:
Python + Crypto Email Academy: https://blog.finxter.com/subscribe/

