{"id":3911,"date":"2018-11-20T16:06:22","date_gmt":"2018-11-20T16:06:22","guid":{"rendered":"https:\/\/www.appservgrid.com\/paw92\/?p=3911"},"modified":"2018-12-07T02:44:36","modified_gmt":"2018-12-07T02:44:36","slug":"python-dash-tutorial-linux-hint","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2018\/11\/20\/python-dash-tutorial-linux-hint\/","title":{"rendered":"Python Dash Tutorial | Linux Hint"},"content":{"rendered":"<p>Hey everybody, Welcome. Dash is the topic that we are going to discuss today. Dash is developed by Plotly. Some of you might have got an idea that Dash is perhaps about graphs because of Plotly. And yes, you are absolutely right. Dash is about representation of graphs in a web UI (user interface). Web UI doesn\u2019t mean that Dash requires an active internet connection to run, rather it just needs a server and will run on \u201clocalhost\u201d or \u201c127.0.0.1\u201d. Dash happens to run on port 8050 by default, so when you run your Dash application on your browser you would go on the address as \u201c127.0.0.1:8050\u201d.<\/p>\n<p>First of all, we have to install Dash on our system. Hit Ctrl+Alt+T on your Ubuntu, it would open up terminal. In order to run Dash applications on our system, we would install 4 to 5 packages using following command:<\/p>\n<p>$ sudo pip install dash dash-renderer dash-html-components dash-core-components plotly<\/p>\n<p>OR<\/p>\n<p>$ sudo -H pip install dash dash-renderer dash-html-components dash-core-components plotly<\/p>\n<p>When you will add -H it would not issue a warning because you would get to the Home variable by using -H in the command. Even if you don\u2019t use it, it would be okay as it would display a warning but Dash would get installed anyway.<\/p>\n<p>Now, you would go on to create a python script. Our first example of code would just display a simple output in our web browser on the server address and port mentioned above. In the example, first 3 lines would be the imports of dash, dash-core-components and dash-html-components respectively. Dash-core-components as dcc means that wherever we want to use dash-core-components we can use \u2018dcc\u2019 instead and similarly where we want to use dash-html-components, we can use \u2018html\u2019. Dash() is the built in class which holds the default code for Dash applications. \u2018app.layout\u2019 represents everything in web UI which means anything you want to display in the browser in Dash application, it has to be written in the operating zone of \u2018app.layout\u2019. Following our first simple code example which just displays a simple output:<\/p>\n<p>import dash<br \/>\nimport dash_core_components as dcc<br \/>\nimport dash_html_components as html<\/p>\n<p>app = dash.Dash()<\/p>\n<p>app.layout = html.Div(&#8216;LinuxHint YouTube Hi&#8217;)<\/p>\n<p>if __name__ == &#8216;__main__&#8217;:<br \/>\napp.run_server(debug=True)<img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/linuxhint.com\/image\/gif;base64,R0lGODdhAQABAPAAAP\/wAAACwAAAAAAQABAEACAkQBADs=\" alt=\"\" width=\"1078\" height=\"105\" \/><\/p>\n<p>Second example is about creating a graph. We would use \u2018dcc\u2019 which essentially means dash-core-components and we would create a graph using it. In our example, we have drawn an example graph of Energy and Time with random values of \u2018x\u2019 and \u2018y\u2019 by giving a type of \u2018line\u2019 to Energy and a type of \u2018bar\u2019 to Time. We would do all of that inside a method dcc.Graph() in which we would name our both axis of the graph and set the title of graph as well.<\/p>\n<h2>Code Example#2:<\/h2>\n<p>import dash<br \/>\nimport dash_core_components as dcc<br \/>\nimport dash_html_components as html<\/p>\n<p>app = dash.Dash()<\/p>\n<p>app.layout = html.Div(children=[<br \/>\nhtml.Div(children=&#8217;LinuxHint Youtube Hi&#8217;),<br \/>\ndcc.Graph(<br \/>\nid=&#8217;graphss&#8217;,<br \/>\nfigure={<br \/>\n&#8216;data&#8217;: [<br \/>\n{&#8216;x&#8217;:[1,2,3,4,5,6,7], &#8216;y&#8217;:[11,12,22,23,24,44,55], &#8216;type&#8217;:&#8217;line&#8217;, &#8216;name&#8217;:&#8217;Energy&#8217;},<br \/>\n{&#8216;x&#8217;:[1,2,3,4,5,6,7], &#8216;y&#8217;:[13,15,26,27,34,44,65], &#8216;type&#8217;:&#8217;bar&#8217;, &#8216;name&#8217;:&#8217;Time&#8217;},<br \/>\n],<br \/>\n&#8216;layout&#8217;: {<br \/>\n&#8216;title&#8217;: &#8216;Graph for Time and Energy&#8217;<br \/>\n}<br \/>\n}<br \/>\n)<br \/>\n])<\/p>\n<p>if __name__ == &#8216;__main__&#8217;:<br \/>\napp.run_server(debug=True)<\/p>\n<p>Pro Tip: While writing python script, use a python IDE or a smart text editor which indents the code automatically for you. Avoid using simple notepad or text editor for python scripts as indentation of code is an important factor in python while running it.<\/p>\n<p><a href=\"https:\/\/www.youtube.com\/watch?v=uspjGquLYgc&amp;t=1s\">I will explain this in more details in video form as well.<\/a><\/p>\n<p><a href=\"https:\/\/linuxhint.com\/python_dash_tutorial\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey everybody, Welcome. Dash is the topic that we are going to discuss today. Dash is developed by Plotly. Some of you might have got an idea that Dash is perhaps about graphs because of Plotly. And yes, you are absolutely right. Dash is about representation of graphs in a web UI (user interface). Web &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2018\/11\/20\/python-dash-tutorial-linux-hint\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Python Dash Tutorial | Linux Hint&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-3911","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/3911","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=3911"}],"version-history":[{"count":1,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/3911\/revisions"}],"predecessor-version":[{"id":4654,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/3911\/revisions\/4654"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=3911"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=3911"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=3911"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}