Exercise: The Dash Architecture
Explore how to build and style interactive Dash web applications by combining HTML components with Bootstrap and Mantine libraries. Learn to structure app layouts, add Plotly visualizations, and create callback functions to update outputs dynamically based on user input.
We'll cover the following...
We'll cover the following...
Exercise 1
Create a Dash app with a dataset of your choice with an HTML header 1 component with the text “My App.” Add a Plotly visual of your choice and place a paragraph below the image saying “Thanks for viewing my app.”
Solution
This code creates a simple web application using Dash, Plotly, and JupyterDash to display a scatter plot based on a dataset of top YouTubers. Here’s a breakdown of the main sections:
-
Load dataset and generate scatter plot:
- Load a CSV file called
top_youtubers.csvon line 2 using pandas and store it in a DataFrame calleddf. - Create a scatter plot using Plotly Express with
video_views_1000son line 3 on the x-axis,subscribers_1000son the y-axis, and black markers. - Update the scatter plot’s layout on line 4, setting the title and centering it.
- Update the marker size to
5on line 5.
- Load a CSV file called
-
Dash app:
- Initialize a JupyterDash app named
app, on line 8. - Define the layout of the app using
html.Divon line 11, which (as a reminder) is a Dash HTML component representing adivin HTML. Inside thehtml.Divcomponent, three child elements are added:html.H1: This creates an H1 header on line 12 with the textMy App. Theidattribute is set tomy-header, which can be used for targeting this element with CSS or JavaScript.dcc.Graph: This is a
- Initialize a JupyterDash app named