{"id":13282,"date":"2021-02-26T16:32:00","date_gmt":"2021-02-26T16:32:00","guid":{"rendered":"https:\/\/www.askpython.com\/?p=13282"},"modified":"2021-03-09T16:32:48","modified_gmt":"2021-03-09T16:32:48","slug":"build-a-dashboard-in-python","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/examples\/build-a-dashboard-in-python","title":{"rendered":"Build a dashboard in python from a CSV file [ Easy Steps ]"},"content":{"rendered":"\n<p>Hello readers! In this tutorial, we&#8217;ll be looking at how we can quickly build a dashboard in Python using dash, from a CSV file.<\/p>\n\n\n\n<p>Dash is a Python framework that makes it easy for anyone to build dashboards in Python, while not having to deal with the frontend required directly.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Steps to build a dashboard in Python<\/h2>\n\n\n\n<p>Let&#8217;s now get started and build a dashboard in Python using the dash library to display data from a CSV File!<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: Plot the data using Plotly<\/h3>\n\n\n\n<p>We&#8217;ll be using a simple CSV file for the data source, namely a COVID time series dataset.<\/p>\n\n\n\n<p>I&#8217;m using <a href=\"https:\/\/www.kaggle.com\/sudalairajkumar\/covid19-in-india\" target=\"_blank\" rel=\"noopener\">this<\/a> COVID-19 dataset from Kaggle. Once you have it ready, we can start using it.<\/p>\n\n\n\n<p>To render the plots, we&#8217;ll be using the <a href=\"https:\/\/www.askpython.com\/python-modules\/python-plotly-tutorial\" class=\"rank-math-link\">Python plotly library<\/a>. To install this library, use:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\npip install plotly\n<\/pre><\/div>\n\n\n<p>Let&#8217;s now plot the time series data for various states. We&#8217;ll use the <a href=\"https:\/\/www.askpython.com\/python-modules\/pandas\/python-pandas-module-tutorial\" class=\"rank-math-link\">Pandas read_csv() function<\/a> to read the data from our CSV dataset. It&#8217;s just 3 simple lines of code!<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport plotly.express as px\n\ndf = pd.read_csv(&#039;covid_19_india.csv&#039;)\n\n# Plot the scatterplot using Plotly. We ploy y vs x (#Confirmed vs Date)\nfig = px.scatter(df, x=&#039;Date&#039;, y=&#039;Confirmed&#039;, color=&#039;State\/UnionTerritory&#039;)\nfig.update_traces(mode=&#039;markers+lines&#039;)\nfig.show()\n<\/pre><\/div>\n\n\n<p>Now plotly should give you a nice visualization of the data. Let&#8217;s now render this in our Dash application.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: Embed the graph with Dash<\/h3>\n\n\n\n<p>To render our dashboard application, we&#8217;ll be using <a class=\"rank-math-link\" href=\"https:\/\/dash.plotly.com\/\" target=\"_blank\" rel=\"noopener\">Dash<\/a>. Install this library using:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\npip install dash\n<\/pre><\/div>\n\n\n<p>We&#8217;ll use dash to render the data in a layout.<\/p>\n\n\n\n<p>Before that, let&#8217;s set up some stylesheets (CSS) for our page to look good! I&#8217;m using the default data from <a href=\"https:\/\/dash.plotly.com\/layout\" class=\"rank-math-link\" target=\"_blank\" rel=\"noopener\">this<\/a> dash official tutorial.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport dash\nimport dash_core_components as dcc\nimport dash_html_components as html\nimport plotly.express as px\nimport pandas as pd\n\nexternal_stylesheets = &#x5B;&#039;https:\/\/codepen.io\/chriddyp\/pen\/bWLwgP.css&#039;]\n\napp = dash.Dash(__name__, external_stylesheets=external_stylesheets)\n\ncolors = {\n    &#039;background&#039;: &#039;#F0F8FF&#039;,\n    &#039;text&#039;: &#039;#00008B&#039;\n}\n<\/pre><\/div>\n\n\n<p>Let&#8217;s now configure our data in this layout.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n# Our dataframe\ndf = pd.read_csv(&#039;covid_19_india.csv&#039;)\n\nfig = px.scatter(df, x=&#039;Date&#039;, y=&#039;Confirmed&#039;, color=&#039;State\/UnionTerritory&#039;)\nfig.update_traces(mode=&#039;markers+lines&#039;)\n\napp.layout = html.Div(children=&#x5B;\n    html.H1(children=&#039;COVID-19 Time Series Dashboard&#039;),\n\n    html.Div(children=&#039;&#039;&#039;\n        COVID-19 Dashboard: India.\n    &#039;&#039;&#039;),\n\n    dcc.Graph(\n        id=&#039;example-graph&#039;,\n        figure=fig\n    )\n])\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Step 3: Run the application server with Flask<\/h3>\n\n\n\n<p>Now, let&#8217;s finally run the application server (via <a href=\"https:\/\/www.askpython.com\/python-modules\/flask\/create-hello-world-in-flask\" class=\"rank-math-link\">Flask<\/a>):<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nif __name__ == &#039;__main__&#039;:\n    app.run_server(debug=True)\n<\/pre><\/div>\n\n\n<p>This will start the server on local port 8050. Let&#8217;s look at the output now, when we go to <code>http:\/\/localhost:8050<\/code><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"369\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/03\/dashboard_output-1024x369.png\" alt=\"Dashboard Output\" class=\"wp-image-13284\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/03\/dashboard_output-1024x369.png 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/03\/dashboard_output-300x108.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/03\/dashboard_output-768x276.png 768w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/03\/dashboard_output-1536x553.png 1536w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/03\/dashboard_output-2048x737.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>Dashboard Output<\/figcaption><\/figure><\/div>\n\n\n\n<p>As you can see, we indeed have a nice looking interactive dashboard in just a few lines of Python code!<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>In this tutorial, we learned how we could build a dashboard in Python from a CSV file using Dash.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n","protected":false},"excerpt":{"rendered":"<p>Hello readers! In this tutorial, we&#8217;ll be looking at how we can quickly build a dashboard in Python using dash, from a CSV file. Dash is a Python framework that makes it easy for anyone to build dashboards in Python, while not having to deal with the frontend required directly. Steps to build a dashboard [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":13286,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-13282","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-examples"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/13282","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=13282"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/13282\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/13286"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=13282"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=13282"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=13282"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}