Dash for Beginners : Python Dashboards
By: Anmol Tomar
Tags: Data Visualization, Dashboard, Python, Dash, Data Science

Dash for Beginners : Python Dashboards

Image designed by freepik

What is Dash?

Dash is a python framework created by plotly for creating interactive web applications. Dash is written on the top of Flask, Plotly.js and React.js. With Dash, you don’t have to learn HTML, CSS and Javascript in order to create interactive dashboards, you only need python. Dash is open source and the application build using this framework are viewed on the web browser.

With Dash, you don’t have to learn HTML, CSS and Javascript in order to create interactive dashboards, you only need python.

Building blocks of Dash

Dash applications are made up of 2 building blocks :

Layout Callbacks

Layout describes the look and feel of the app, it defines the elements such as graphs, dropdowns etc and the placement, size, color etc of these elements. Dash contains Dash HTML components using which we can create and style HTML content such as headings, paragraph, images etc using python. Elements such as graphs, dropdowns, sliders are created using Dash Core components.

Callbacks are used to bring interactivity to the dash applications. These are the functions using which, for example, we can define the activity that would happen on clicking a button or a dropdown.

Layouts using Dash

Now, let’s have a look at how we can create web based layouts using plotly Dash. Before starting with the layout, let’s install some required packages.

Now, we will import dash package, dash_html_components for HTML classes, dash_core_components for elements such as graph, dropdown etc, and plotly packages for creating plots and reading stock prices dataset.

(In the below code) We are initializing our dash app using dash package. Then, we are reading the stock prices data for different companies from 2018 to 2019. We are creating stock_prices function that returns the line chart for Google’s stock prices.

(In the above code) At line 16, we are setting our layout using html Div component, which is a kind of wrapper within which the elements(heading, graph) of the layout will be created. The Div component contains arguments such as id (a unique identifier of the element), style (for setting the width, height, color etc) and children (equal to square bracket within which elements of the layout are initialised).

Inside the children component (of html.Div) we are creating html H1 heading at line 17 using H1 function. Inside the function, we are setting the unique id of the function (id = ‘H1’), children property using which we set the text of the heading and style property as a dictionary within which we are setting styling such as center aligning the text, setting top and bottom margin to 40 pixel. At line 21, we are using dash core component (dcc) to create graph , where we are setting the id of the graph and the figure argument, which is equal to the function call (stock_pricest()) that returns the plotly figure object.

In order to view our application, we need to run our web server just like in Flask. Remember Dash is built on top of Flask.

On running the app, you will see that the app is running on http://127.0.0.1:8050/ , which is your local server. Copy this url and paste it in your browser and you will see the below vizualisation.

Now, we will create a dropdown using dash core components. Using the dropdown, we will be able to select the stocks of Google, Apple or Amazon.

Dropdowns are created using the Dropdown() function, which has the following arguments -

id — Unique identifier of the dropdown. options — Sets the ‘label’ (the text visible in the dropdown) and ‘value’ (used by dash to communicate with callbacks) as key value pair. value — default selection for the dropdown.

Callbacks in Dash: Making it interactive

Now, let’s have a look at how we can create the callback that will connect the dropdown and the stock prices line chart.

A callback is initialised using @app.callback(), which is followed by a function definition. Within this function, we define what happens on changing the value of the dropdown.

Let’s have a look at the arguments within the callback function :

Output: This is used to define the components within the layout which will be updated when the function below the callback (graph_update()) return some object. Output function takes 2 arguments — 1) component_id defines the id of the component we want to update with our function graph_update. We want to update stock prices chart within the dcc.Graph, so we will set the component id to ‘line_plot’, which is id of our graph component. 2) Component property defines the property of the component that will be updated, which is figure property of dcc.Graph in our layout. Input: This is used to define the components, the change in whose value will trigger the callback. Input function also takes component_id and component_property as argument. We want the callback to get triggered based on change in the value of our dropdown, so we set the component_property to ‘value’ property of dropdown. Please note that Input is defined within a list.

The component property of Input function, which is ‘value’ of the dropdown, goes as an argument within the function graph_update. Inside the function we are creating scatter plot and returning the figure object fig, which is passed to the figure property of dcc.Graph using the Output function of the callback.

Bringing it all together

Now let’s combine the layout, the dropdown and the callback in the below code :

The image below shows how the change in the value of dropdown updates our stock prices line chart.

Summary

So, we looked at how we can create interactive dashboards using plotly Dash. Firstly, we created the layout using html components and dash core components. Then, we added dropdown and connected it with the stock prices line chart using callback.