Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Interactive Dashboards and Data Apps with Plotly and Dash

You're reading from   Interactive Dashboards and Data Apps with Plotly and Dash Harness the power of a fully fledged frontend web framework in Python – no JavaScript required

Arrow left icon
Product type Paperback
Published in May 2021
Publisher Packt
ISBN-13 9781800568914
Length 364 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Elias Dabbas Elias Dabbas
Author Profile Icon Elias Dabbas
Elias Dabbas
Arrow right icon
View More author details
Toc

Table of Contents (18) Chapters Close

Preface 1. Section 1: Building a Dash App
2. Chapter 1: Overview of the Dash Ecosystem FREE CHAPTER 3. Chapter 2: Exploring the Structure of a Dash App 4. Chapter 3: Working with Plotly's Figure Objects 5. Chapter 4: Data Manipulation and Preparation, Paving the Way to Plotly Express 6. Section 2: Adding Functionality to Your App with Real Data
7. Chapter 5: Interactively Comparing Values with Bar Charts and Dropdown Menus 8. Chapter 6: Exploring Variables with Scatter Plots and Filtering Subsets with Sliders 9. Chapter 7: Exploring Map Plots and Enriching Your Dashboards with Markdown 10. Chapter 8: Calculating the Frequency of Your Data with Histograms and Building Interactive Tables 11. Section 3: Taking Your App to the Next Level
12. Chapter 9: Letting Your Data Speak for Itself with Machine Learning 13. Chapter 10: Turbo-charge Your Apps with Advanced Callbacks 14. Chapter 11: URLs and Multi-Page Apps 15. Chapter 12: Deploying Your App 16. Chapter 13: Next Steps 17. Other Books You May Enjoy

Creating and running the simplest app

Using the structure that we just discussed, and excluding callback functions, let's now build our first simple app!

Create a file and name it app.py, and write the following code:

  1. Import the required packages using their usual aliases:
    import dash
    import dash_html_components as html
  2. Create (instantiate) the app:
    app = dash.Dash(__name__)
  3. Create the app's layout:
    app.layout = html.Div([
        html.H1('Hello, World!')
    ])
  4. Run the app:
    if __name__ == '__main__':
        app.run_server(debug=True)

A few points before running the app. First, I strongly suggest that you don't copy and paste code. It's important to make sure you remember what you coded. It's also useful to explore the possibilities provided by each component, class, or function. Most IDEs provide hints on what is possible.

This app's layout contains one element, which is the list passed to html.Div, corresponding to its children parameter. This will produce an H1 element on the page. Finally, note that I set debug=True in the app.run_server method. This activates several developer tools that are really useful while developing and debugging.

You are now ready to run your first app. From the command line, in the same folder where you saved your app file, run this:

python app.py 

You might need to run the preceding command using python3 if your system is not configured to use version three by default:

python3 app.py

You should now see an output like that shown in Figure 1.3, indicating that the app is running:

Figure 1.3 – Command-line output while running the app

Figure 1.3 – Command-line output while running the app

Congratulations on running your very first Dash app! Now, if you point your browser to the URL shown in the output, http://127.0.0.1:8050, you should see the "Hello, World!" message in H1 on the page. As you can see, it shows that it is serving a Flask app called "app," with a warning that this server is not designed for production use. We will cover deployment in a later chapter, but this server is good enough for developing and testing your apps. You can also see that we are in debug mode:

Figure 1.4 – App rendered in the browser

Figure 1.4 – App rendered in the browser

As specified, we see the text in H1, and we can also see the blue button as well. Clicking on this button will open some options in the browser, and it will be more useful once there are callback functions and/or errors while running the app. We wouldn't have gotten the blue button if we had run the app with debug=False, which is the default.

Now that we have established a good-enough understanding of the main elements that go into creating a Dash app, and we have run a minimal one, we are ready to explore two packages that are used for adding and managing visible elements: first, Dash HTML Components, and after that, we will explore how to use Dash Bootstrap Components.

You have been reading a chapter from
Interactive Dashboards and Data Apps with Plotly and Dash
Published in: May 2021
Publisher: Packt
ISBN-13: 9781800568914
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image