<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Satyndra Gautam on Medium]]></title>
        <description><![CDATA[Stories by Satyndra Gautam on Medium]]></description>
        <link>https://medium.com/@gautam.ishu5?source=rss-15d6a2c2c100------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*46WtfMFxowdmEcjqljKfNw.jpeg</url>
            <title>Stories by Satyndra Gautam on Medium</title>
            <link>https://medium.com/@gautam.ishu5?source=rss-15d6a2c2c100------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 09:56:23 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@gautam.ishu5/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How to Structure and Organize Your Python Project (Dash Apps) in a Modular Fashion]]></title>
            <link>https://medium.com/@gautam.ishu5/how-to-structure-and-organize-your-python-project-dash-apps-in-a-modular-fashion-1f5586d88fa7?source=rss-15d6a2c2c100------2</link>
            <guid isPermaLink="false">https://medium.com/p/1f5586d88fa7</guid>
            <category><![CDATA[project-management]]></category>
            <category><![CDATA[dash]]></category>
            <category><![CDATA[project-development]]></category>
            <category><![CDATA[modularity]]></category>
            <category><![CDATA[web-app-development]]></category>
            <dc:creator><![CDATA[Satyndra Gautam]]></dc:creator>
            <pubDate>Wed, 28 Aug 2024 18:19:21 GMT</pubDate>
            <atom:updated>2024-08-28T18:19:21.556Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*41QyEJZtwdH0WMe_vJTBCQ.png" /></figure><p>Ever wonder how to structure and organize your files and scripts when your Python project grows? If the project’s folders, files, and scripts are cluttered or your one folder/script contains everything, then this results in headaches and a nightmare and hard to find certain code line or scripts when you need. Speaking from experience, in my first end-to-end Dash app project, <a href="https://my-first-plotly-dash-app.onrender.com/">“Explore US Exploration”</a>, I used one script with over 23k lines of code, which quickly became overwhelming and unmanageable despite I was hardly able to find whatever updates were needed. However, this is definitely not RECOMMENDED.</p><p>There are numerous articles out there detailing how to structure Python projects, which are very well-detailed. However, I’ve found out that some individuals still don’t create components/contents folders or separate scripts for callbacks page-wise. So, how do I structure and organize my projects now? I’ve learned that separating components, callbacks, scripts, CSS files page-wise into distinct folders with respect to pages makes a huge difference. This guide provides a step-by-step approach to creating a modular, organized project structure along with practical examples for a multi-page web-based application using Plotly Dash.</p><h4>Project Structure</h4><p>Always start with a basic approach, creating a project folder with a meaningful name. I am using <em>‘personal_budget_management_tool’.</em></p><p><strong><em>● Create a Project Folder with a Starting Point of Files and Imports:</em></strong></p><pre>Create your project directory -&gt; <br>mkdir personal_budget_management_tool (command prompt)<br><br>Navigate to the project folder -&gt; <br>cd personal_budget_management_tool (command prompt)<br><br>--&gt; if you wish to initialize virtual environment for your project, <br>then follow the steps, <br><br>Intialize Virtual Env: <br>python -m venv venv (command prompt) &lt;--- Eventually you change your virtual env folder name <br><br>Activate your virtual env: <br>source venv/bin/activate <br><br>Install all the libraries you need for your project. For example;<br><br>pip install dash <br>pip install pandas<br>pip install numpy<br>pip install dash_bootstrap_components<br>pip install humanize <br>pip install python-dotenv <br>.<br>.<br>. <br>and so on </pre><p><strong><em>● Add Main.py &amp; requirements.txt: </em></strong>Please note that it is up to you whether you want to create virtual environment or not. However, you can still initialize your project directly and create main.py and requirements.txt file in the root directory.</p><pre>personal_budget_management_tool<br>        └── venv<br>        └── main.py<br>        └── requirements.txt</pre><p><strong><em>● Create Pages folder and Pages Script: </em></strong>Create pages folder and pages script if multi-pages</p><pre>personal_budget_management_tool<br>        └── pages<br>                └── page1.py <br>                └── page2.py <br>                └── page3.py<br>                └── page4.py <br>                └── page5.py  <br>        └── venv<br>        └── main.py<br>        └── requirements.txt</pre><p><strong><em>● Create Assets &amp; CSS Styles and Subpages: </em></strong>If subpages are needed, create folders for your Nav links that need to have subpages. Oops, we need to add an assets folder that is essential for any web-based application development. In this folder, create a global styles.css file for the base layout structure for the app and create an images folder. Later on, we’ll add CSS files according to each page in a few moments.</p><pre>personal_budget_management_tool<br>        └── pages<br>                └── page1.py <br>                └── page2.py <br>                └── page3.py<br>                └── page4.py <br>                └── page5.py<br>                └── page2_subs <br>                        └── subpage1.py<br>                        └── subpage2.py <br>                └── page5_subs<br>                        └── subpage1.py<br>                        └── subpage2.py  <br>        └── assets<br>            └── images<br>            └── styles.css<br>        └── venv<br>        └── main.py<br>        └── requirements.txt</pre><p><strong><em>● Add App Folder, Sub-Folders, and Base Layout &amp; Callbacks Scripts: </em></strong>At this point, you’ve successfully structured and organized your important files (main.py) along with requirements.txt, pages, subpages, and assets and CSS file. Now, create one parent folder as ‘app’ (this one is very important) and create a couple of sub-folders, scripts, and such as below;</p><pre>personal_budget_management_tool<br>        └── pages<br>            └── page1.py <br>            └── page2.py <br>            └── page3.py<br>            └── page4.py <br>            └── page5.py<br>            └── page2_subs <br>                └── subpage1.py<br>                └── subpage2.py <br>            └── page5_subs<br>                └── subpage1.py<br>                └── subpage2.py<br>        └── app<br>            └── auth (name it in whatever way you like - admin panels/login system, etc.)<br>            └── pages_layouts <br>            └── main_app_base_layout.py (I love to keep it here)<br>            └── main_app_base_layout_callbacks.py <br>            └── __init__.py <br>        └── assets<br>            └── images<br>            └── styles.css <br>        └── venv<br>        └── main.py<br>        └── requirements.txt</pre><p><strong><em>● Back-End Jobs folder and files: </em></strong>Looking cool! right? Now, let’s add a back-end jobs related folder — Data, DBs, etc.</p><pre>personal_budget_management_tool<br>        └── pages<br>            └── page1.py <br>            └── page2.py <br>            └── page3.py<br>            └── page4.py <br>            └── page5.py<br>            └── page2_subs <br>                └── subpage1.py<br>                └── subpage2.py <br>            └── page5_subs<br>                └── subpage1.py<br>                └── subpage2.py<br>        └── app<br>            └── auth (you can name it in whatever way you like )<br>            └── pages_layouts <br>            └── main_app_base_layout.py ( I love to keep it here)<br>            └── main_app_base_layout_callbacks.py <br>            └── __init__.py  <br>        └── assets<br>            └── images<br>            └── styles.css <br>        └── data<br>            └── data_file_01.csv <br>            └── data_file_02.csv <br>            └── data_file_03.csv <br>        └── venv<br>        └── main.py<br>        └── requirements.txt</pre><p><strong><em>● Create Layout/Callbacks folder by Pages and add scripts:</em></strong><em> </em>We’ve done great job so far. As I mentioned above, the cleaner the structure of your project is, the lesser the hurdles and hardships are, resulting in a thoughtful project structure and clean &amp; scalable codebase. Now, let’s create parent folders for each page containing scripts for layout and callbacks as below;</p><pre>personal_budget_management_tool<br>        └── pages<br>            └── page1.py <br>            └── page2.py <br>            └── page3.py<br>            └── page4.py <br>            └── page5.py<br>            └── page2_subs <br>                └── subpage1.py<br>                └── subpage2.py <br>            └── page5_subs<br>                └── subpage1.py<br>                └── subpage2.py<br>        └── app<br>            └── auth (you can name it in whatever way you like )<br>            └── pages_layouts<br>                └── page1<br>                    └── page1_layout.py <br>                    └── page1_callbacks.py <br>                └── page2<br>                    └── page2_layout.py <br>                    └── page2_callbacks.py <br>                └── page3<br>                     └── page3_layout.py <br>                    └── page3_callbacks.py <br>                └── ..... and so on <br>            └── main_app_base_layout.py ( I love to keep it here)<br>            └── main_app_base_layout_callbacks.py <br>            └── __init__.py  <br>        └── assets<br>            └── images<br>            └── styles.css <br>        └── data<br>            └── data_file_01.csv <br>            └── data_file_02.csv <br>            └── data_file_03.csv <br>        └── venv<br>        └── main.py<br>        └── requirements.txt</pre><p>Please note that you can directly use the callbacks functions in the page_layout.py file. However, to avoid conflict and such, I always separate my contents and callbacks by pages, allowing me to keep my code in a modular fashion and concise way. However, you can do whatever you want, up to your project’s structure. If your project doesn’t involve nested layers, then it’s fine to use callback functions in the layout script directly. Sometimes, I think keeping things clean is always a good practice and modularizing the project structure and separating concerns like callbacks, layouts, and even CSS per page is smart and effective for scaling and maintainability.</p><p>Let’s add CSS file for each page. NO WORRIES! I will show you how to use these scripts later on.</p><p><strong><em>● Add CSS styles files by Pages and other scripts:</em></strong> Add CSS files according to each page except base layout(which already contains styles.css), login_and_sign_up_form.py script, and add more data files.</p><pre>personal_budget_management_tool<br>        └── pages<br>            └── page1.py <br>            └── page2.py <br>            └── page3.py<br>            └── page4.py <br>            └── page5.py<br>            └── page2_subs <br>                └── subpage1.py<br>                └── subpage2.py <br>            └── page5_subs<br>                └── subpage1.py<br>                └── subpage2.py<br>        └── app<br>            └── auth (you can name it in whatever way you like )<br>                └── login_and_sign_up_form.py <br>            └── pages_layouts<br>                └── page1<br>                    └── page1_layout.py <br>                    └── page1_callbacks.py <br>                └── page2<br>                    └── page2_layout.py <br>                    └── page2_callbacks.py <br>                └── page3<br>                    └── page3_layout.py <br>                    └── page3_callbacks.py <br>                └── ..... and so on <br>            └── main_app_base_layout.py ( I love to keep it here)<br>            └── main_app_base_layout_callbacks.py <br>            └── __init__.py  <br>        └── assets<br>            └── images<br>            └── styles.css<br>            └── page1.css<br>            └── page2.css<br>            └── page3.css<br>            └── ...... and so on <br>        └── data (you may encounter various types of data files)<br>            └── data_file_01.csv <br>            └── data_file_02.json<br>            └── data_file_03.txt<br>            └── data_file_04.xlsx<br>            └── data_file_05.parquet<br>        └── venv<br>        └── main.py<br>        └── requirements.txt</pre><p><strong><em>● Reusable Components:</em></strong> Finally, we need to add components folder where we will store our REUSABLE components. Meaning, we can use these components over and over again in our app. For instance, sidebar.py, login_form.py, pie_chart, bar_chart, histogram, heatmap, folium, and applications widgets such as sliders, dropdowns, inputs, checkboxes, etc.</p><pre>personal_budget_management_tool<br>        └── pages<br>            └── page1.py <br>            └── page2.py <br>            └── page3.py<br>            └── page4.py <br>            └── page5.py<br>            └── page2_subs <br>                └── subpage1.py<br>                └── subpage2.py <br>            └── page5_subs<br>                └── subpage1.py<br>                └── subpage2.py<br>        └── app<br>            └── auth (you can name it in whatever way you like )<br>                └── login_and_sign_up_form.py<br>            └── components<br>                └── pie_chart.py<br>                └── bar_chart.py<br>                └── histogram_chart.py<br>                └── folium_chart.py<br>                └── heatmap_chart.py<br>                └── sliders_chart.py<br>                └── dropdowns_chart.py<br>                └── ........ and so on <br>            └── pages_layouts<br>                └── page1<br>                    └── page1_layout.py <br>                    └── page1_callbacks.py <br>                └── page2<br>                    └── page2_layout.py <br>                    └── page2_callbacks.py <br>                └── page3<br>                    └── page3_layout.py <br>                    └── page3_callbacks.py <br>                └── ..... and so on <br>            └── main_app_base_layout.py ( I love to keep it here)<br>            └── main_app_base_layout_callbacks.py <br>            └── __init__.py  <br>        └── assets<br>            └── images<br>            └── styles.css<br>            └── page1.css<br>            └── page2.css<br>            └── page3.css<br>            └── ...... and so on <br>        └── data (you may encounter various types of data files)<br>            └── data_file_01.csv <br>            └── data_file_02.json<br>            └── data_file_03.txt<br>            └── data_file_04.xlsx<br>            └── data_file_05.parquet<br>        └── venv<br>        └── main.py<br>        └── requirements.txt</pre><p>Now we’ve created a comprehensive and modularized structure of a Python Project (Dash App).</p><h4>Add Testing Scripts</h4><p>When developing your application, you need to ensure that it works as expected, especially when you need to modify the function. One way to ensure that any changes you make to a function or any other functions that it depends on is to write tests around that code. Additionally, along the way of developing your application, you might encounter learning so much stuffs and you want to keep it for future reference, you can create a folder as ‘others’;</p><pre>personal_budget_management_tool<br>        └── pages<br>            └── page1.py <br>            └── page2.py <br>            └── page3.py<br>            └── page4.py <br>            └── page5.py<br>            └── page2_subs <br>                └── subpage1.py<br>                └── subpage2.py <br>            └── page5_subs<br>                └── subpage1.py<br>                └── subpage2.py<br>        └── app<br>            └── auth (you can name it in whatever way you like )<br>                └── login_and_sign_up_form.py<br>            └── components<br>                └── pie_chart.py<br>                └── bar_chart.py<br>                └── histogram_chart.py<br>                └── folium_chart.py<br>                └── heatmap_chart.py<br>                └── sliders_chart.py<br>                └── dropdowns_chart.py<br>                └── ........ and so on <br>            └── pages_layouts<br>                └── page1<br>                    └── page1_layout.py <br>                    └── page1_callbacks.py <br>                └── page2<br>                    └── page2_layout.py <br>                    └── page2_callbacks.py <br>                └── page3<br>                    └── page3_layout.py <br>                    └── page3_callbacks.py <br>                └── ..... and so on <br>            └── main_app_base_layout.py ( I love to keep it here)<br>            └── main_app_base_layout_callbacks.py <br>            └── __init__.py  <br>        └── assets<br>            └── images<br>            └── styles.css<br>            └── page1.css<br>            └── page2.css<br>            └── page3.css<br>            └── ...... and so on <br>        └── data (you may encounter various types of data files)<br>            └── data_file_01.csv <br>            └── data_file_02.json<br>            └── data_file_03.txt<br>            └── data_file_04.xlsx<br>            └── data_file_05.parquet<br>        └── venv<br>        └── others<br>            └── test_calculation.py (Honestly, I keep one or two scripts for testing)<br>            └── test_function_01.py <br>            └── learning_stuffs.py <br>            └── .......... and other files and scripts<br>        └── main.py<br>        └── requirements.txt</pre><p>Now, this is the final structure of my web-based application project using Python and Dash. I believe this modular approach promotes cleaner code and simplifies maintenance.</p><h4><strong>How to interact between scripts and make the app functional</strong></h4><p>Before we dive in practical applications of how we can use our scripts and files and make our apps functional, here is one of my ongoing projects.</p><p><strong><em>My Project Structure:</em></strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*T-4g0YCganmvT1mQoej7tA.png" /><figcaption>My Project Structure</figcaption></figure><p><strong>Real Examples:</strong></p><p>The snap above I hope will help you understand the structure now. Now, we will go through some examples. Let’s say we want to implement a piece of layout in home.py from home_layout.py which is located in the app/home/ folder. Additionally, we will import the pie plot from the pie_chart.py script located in the components folder.</p><p><strong><em>STEPS:</em></strong> Go to home_layout.py script -&gt; Implement Python functions and under the functions, define layouts and contents -&gt; Go to home.py script -&gt; Import the functions we created -&gt; Integrate these functions wherever you want in your home.py -&gt; Refresh or run the main.py or (app.py) file.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kLnhW8vjXRtdLpZ5hWHKWA.png" /><figcaption>Layouts Rendering</figcaption></figure><p>With the image above, we can see that we first defined some layouts in the home_layout.py and then, returned the outputs in the home.py. Let’s update our home_layout.py for pie chart integration. Please note that we will use the same figure for other example_function.</p><p><strong><em>Pie Chart Integration:</em></strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*n6iJp0-gY7w8Ra-hK7NjyA.png" /></figure><p><strong><em>Please note;</em></strong></p><ul><li>Structuring and Organizing Python projects and files entirely depends on the complexity of the projects and individuals.</li><li>It is also entirely up to you to either keep layouts and callbacks in the parent Pages or create separate scripts just like I’ve created in my project.</li><li>If you think you don’t particularly need to create callbacks.py scripts, then you can still create your callback functions in layout.py or page.py script. However, modularity always motivates a cleaner codebase, allowing us to find things easily. So, I second this.</li></ul><h4>Summary</h4><p>As Python projects grow, it becomes extremely important to structure and organize your files and scripts. A cluttered project can become a nightmare, making it difficult to maintain and scale. so, ensuring your Dash app is well organized can go a long way to helping you stay sane when developing your app. By separating pages, layouts, styles, and components (Modularity), this does not only saves you time but also results in a thoughtful project structure — clean, scalable, maintainable, and enjoyable.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1f5586d88fa7" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to create an Animated Pie Chart using Plotly]]></title>
            <link>https://medium.com/@gautam.ishu5/how-to-create-an-animated-pie-chart-using-plotly-06513b1318c8?source=rss-15d6a2c2c100------2</link>
            <guid isPermaLink="false">https://medium.com/p/06513b1318c8</guid>
            <category><![CDATA[data-visualization]]></category>
            <category><![CDATA[data-storytelling]]></category>
            <category><![CDATA[pie-charts]]></category>
            <category><![CDATA[plotly]]></category>
            <category><![CDATA[data-science]]></category>
            <dc:creator><![CDATA[Satyndra Gautam]]></dc:creator>
            <pubDate>Mon, 26 Aug 2024 23:37:26 GMT</pubDate>
            <atom:updated>2024-08-26T23:37:26.587Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/648/1*5AgrJEF0X7xgpY6SIWk8Pw.png" /></figure><h4><strong>TL;DR</strong></h4><p>This endeavor shows how to create interactive and visually appealing pie charts using Python and Plotly. This includes exploring Plotly express and graph_objects components including the addition of traces, customizing titles, axes, layout, tooltips/hover, donuts, group slices, and configuring different simple HTML tags.</p><p>Feel free to visit the notebook as an HTML page: <a href="https://sites.google.com/view/gautamsatyndra/blog/plotly-animated-pie-chart">Animated Pie Chart</a></p><h4>Data Visualization and Pie Chart</h4><p>In today’s data-driven world, numbers alone can numb the mind, but visuals ignite insight. Data visualization directly speaks to our minds. It transforms complex information into captivating stories, revealing hidden patterns, trends, and truths.</p><p>There are numerous ways to create visualizations through a wide range of plots and graphs. One of them is the pie chart which is used to display numerical proportions, with each slice representing a category and its size corresponding to the percentage of the whole, illustrating how individual parts contribute to a whole lot.</p><h4>Plotly</h4><p>Plotly is a free, open-source, browser-based graphing library that allows users to create interactive graphs. It’s powered by plotly.js, a JavaScript library, and is available for Python, R, Julia, Node.js, etc. Plotly supports many charts including pie chart, line plots, scatter plots, bar charts, histograms, heatmaps, subplots, multiple axes, etc.</p><blockquote><strong>Plotly Express</strong></blockquote><blockquote>Plotly Express is a high-level interface for data visualization in Python. It provides a simple and concise way to create a wide range of interactive graphs and charts with just a few lines of code. It’s particularly suited for rapid development and straightforward use cases.</blockquote><blockquote><strong>Plotly Graph Objects</strong></blockquote><blockquote>Plotly Graph Objects is a lower-level interface for data visualization in Python, providing more fine-grained control over the appearance and behavior of graphs. It allows us to customize every aspect of your visualizations, from the layout and design to the interactions and animations. This is what we will use in this notebook.</blockquote><h4>Chart Development</h4><p>In this section, we will utilize both express and graph objects to create two different basic pie charts incorporating a few props such as colors, titles, hover text, etc. However, we will customize the pie chart heavily later on using key attributes, and then finally create a reusable function with animation and visual effects, which we can use to return the animated pie chart.</p><p><strong>● Import libraries and data</strong></p><pre>#import libraries<br><br>import pandas as pd <br>import plotly.express as px<br>import plotly.graph_objects as go<br><br># load the gapminder dataset<br>df = px.data.gapminder()<br><br>df = df[df[&#39;year&#39;] == 2007].groupby(&#39;continent&#39;)[&#39;pop&#39;].sum().reset_index()<br>df = df.sort_values(&#39;pop&#39;, ascending=False).reset_index(drop=True)<br>df.head()</pre><p><strong>● Pie chart using Express</strong></p><pre># define slice colors <br>custom_colors = [<br>    &#39;#443a59&#39;,<br>    &#39;#ee4a60&#39;,<br>    &#39;#df9f43&#39;,<br>    &#39;#d6d4d4&#39;,<br>    &#39;#166edc&#39;<br>]<br><br># create fig object with some initial props <br>fig_px = px.pie(<br>    df, <br>    values=&#39;pop&#39;, <br>    names=&#39;continent&#39;,<br>    title=&#39;Population Distribution by Continent (2007)&#39;,<br>    # width=400, # helps wit plot size <br>    # height=400,<br>    color_discrete_sequence=custom_colors,<br>)<br><br>custom_title = &quot;&quot;&quot;<br>Plotly Express: Population Distribution by Continent (2007)&lt;br&gt;<br>&lt;b&gt;&lt;span style=&quot;font-size: 13px; color: red;&quot;&gt;Data from Gapminder&lt;/span&gt;&lt;br&gt;<br>&quot;&quot;&quot;<br><br>fig_px.update_layout(<br>    title={ <br>        &#39;text&#39;: custom_title,<br>    },<br>    autosize=True, # automatically controls the chart size<br>    margin=dict(l=0, r=0, t=90, b=40),  # control the margin around the plot <br>    paper_bgcolor=&quot;rgba(247, 229, 247, 0.308)&quot;,<br>    showlegend=True, # (boolean - show or hide),<br>)<br><br># the following configuration will remove the icons from the plot<br>fig_px.show(config={&#39;displayModeBar&#39;: False})</pre><p><strong><em>Result;</em></strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*L59TCdgk-d-eFbg_nV9B5g.png" /><figcaption>Using Plotly Express</figcaption></figure><p>We can see that we have custom titles and sector colors. Since we are going to use graph_objects from now on. Let’s create the same chart using graph object package. Please note;</p><p><strong>● Pie chart using Graph Objects</strong></p><pre>import plotly.graph_objects as go<br><br>labels=df[&#39;continent&#39;].unique() # in case, avoid duplicates<br>values=df[&#39;pop&#39;]<br>total=sum(values)<br><br># create fig object <br>fig_go=go.Figure(<br>    data=[<br>        go.Pie(<br>            labels=labels, <br>            values=values, <br>            marker=dict(<br>                colors=[custom_colors[i] for i in range(len(labels))],<br>            ), <br>        )<br>    ]<br>)<br><br>fig_go.update_layout(<br>    title={ <br>        &#39;text&#39;: custom_title, # use the same title define above <br>    },<br>    autosize=True, <br>    margin=dict(l=0, r=0, t=90, b=40),  <br>    paper_bgcolor=&quot;rgba(247, 229, 247, 0.308)&quot;, # set your background color if needed <br>    showlegend=True, <br>)<br><br><br>fig_go.show()</pre><p><strong><em>Result;</em></strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QLQQJv7GMNh9yErPTrhrNA.png" /><figcaption>Using Plotly Graph Objects</figcaption></figure><p>The key difference :</p><p><strong>Plotly Express</strong> requires DataFrame as an input, making it easier to create visualizations by mapping DataFrame columns to chart elements.</p><p><strong>Plotly Graph Objects</strong> does not require DataFrame as an input and is more flexible, allowing you to specify data directly as lists, arrays, or other structures.</p><p><strong>● Donut Chart:</strong></p><p>With just one parameter, we can convert a pie chart into donut chart using <strong>“Hole”</strong>.</p><pre>import humanize # used for formatting the number <br>formatted_total = humanize.intword(total).replace(&#39;million&#39;, &#39;M&#39;).replace(&#39;billion&#39;, &#39;B&#39;)<br><br>fig_donut = fig_go.update_traces(hole=0.6)<br>fig_donut.update_layout(<br>    annotations=[<br>        dict(<br>            text= f&quot;&lt;b&gt;Total&lt;/b&gt;&lt;br&gt; {formatted_total}&quot;, <br>            x=0.5, <br>            y=0.5, <br>            font_size=18, <br>            showarrow=False,<br>            font_family= &#39;Lexend&#39;,<br>            # font_weight= &#39;bold&#39;,<br>        )<br>    ],<br>    paper_bgcolor=&quot;rgba(0, 0, 0, 0)&quot;,<br>)<br><br>fig_donut.show()</pre><p><strong><em>Result:</em></strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KK_8XP4HBBdCMrkqiV29AQ.png" /><figcaption>Donut Chart</figcaption></figure><p>We can see that we have successfully converted our pie chart into a donut chart with more customizations incorporating the total population of continents in the middle with formatted values. Additionally, whether you know much about HTML or not, with just some simple HTML tags, you can make your Plotly charts interactive and appealing.</p><p><strong>Annotations</strong> are very powerful. You can use them to customize your texts, titles, metrics, values, and outputs via interactive widgets, etc. in a more meaningful and concise way.</p><p>We can further make our chart more visually <strong>informative</strong> by introducing the <strong>‘Pull’ </strong>attribute, which will highlight key information by showing which label has the highest or lowest proportion.</p><p>-&gt; Use custom pulls if you don’t want to dynamically get pulls with highest or lowest or else.</p><pre># USE fig_go and create a new fig object <br>fig_pulled_out = fig_go.update_traces(<br>    hole=0,<br>    pull=0.2, # if all the pie slices need to be pulled out <br>)<br><br>custom_pulls = [ 0, 0, 0.1, 0.2, 0 ]<br><br># update your chart with custom pulls <br><br>fig_pulled_out.update_traces(<br>    pull = custom_pulls, #this will result in 3rd and 4th slice to be pulled-out<br>)<br><br># use maximum value and get the slice pulled out <br>max_value = max(values) <br>max_pull = [0.15 if value == max_value else 0 for value in values]<br><br># update your trace again <br>fig_pulled_out.update_traces(<br>    pull = max_pull, # dynamically pull presenting maximum proportion <br>)<br><br>fig_pulled_out.update_layout(<br>    paper_bgcolor=&quot;rgba(0, 0, 0, 0)&quot;, # set no background color<br>)<br><br># display <br>fig_pulled_out.show()</pre><p><em>Result;</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eRvGXbQ_ISESrW95b3bsjA.png" /><figcaption>Pie chart with pulled-out with max proportion</figcaption></figure><p>We’ve used some of the customization stuff. Next, we will learn how to map custom_colors to text labels in a same way how we assigned the colors to slices earlier. Just use;</p><pre>fig_pulled_out.update_traces(<br>    textposition=&#39;outside&#39;,<br>    textfont=dict(<br>        size=15, # size of font<br>        family=&#39;montserrat regular&#39;, # font family <br>        color=[custom_colors[i] for i in range(len(labels))], # customized labels with matching color <br>        weight= &#39;bold&#39;<br>    ),<br>    hoverinfo=&quot;label+value&quot; # on hover, get label and values <br>)</pre><p><strong>Legend Position:</strong> we can use x, y, anchors, and orientation to adjust the position of Legends. Additionally, if you don’t want legends on the figure, use showlegend= False in the update_layout(). Apparently, you can find all these stuffs on the Plotly website.</p><pre># we can use anchors and x &amp; y positions to control the position of the legend<br>fig_pulled_out.update_layout(<br>    legend=dict(<br>        yanchor=&quot;top&quot;,<br>        y=0.91,<br>        xanchor=&quot;left&quot;,<br>        x= -0.35<br>    )<br>)<br><br># however, for this endeavor, we would like to set the legend on the bottom and in the center...<br>fig_pulled_out.update_layout(<br>    legend=dict(<br>        yanchor=&quot;bottom&quot;,<br>        y= -0.215,<br>        xanchor=&quot;center&quot;,<br>        x= 0.53,<br>        orientation=&quot;h&quot;,<br>        entrywidth=70, # used for gapping between legends horizontally<br>        bgcolor=&quot;rgba(0, 0, 0, 0)&quot;,<br>    ),<br>    margin=dict(l=0, r=0, t=70, b=40),<br>    autosize=True,<br>    plot_bgcolor=&#39;rgba(0, 0, 0, 0)&#39;,<br>    paper_bgcolor=&#39;rgba(0, 0, 0, 0)&#39;,<br>)<br><br>fig_pulled_out.show(config={&#39;displayModeBar&#39;: False}) # config helps hiding the icons on the plot </pre><p><em>Result;</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WPbzzkNsa6lTTI0WK0xPKQ.png" /><figcaption>Pie chart with adjusted legend position and text colors</figcaption></figure><p>Similarly, we can adjust the position of titles, texts, hover texts, font, colors and much more.</p><h4>Reusable Function to create an Animated Pie Chart</h4><p>I believe you’ve probably grasped how to customize Plotly figures using attributes and props. Since this post seems to be long enough and I don’t want to bore readers. If you wish to learn how each element has been individually configured in the chart with details, please visit the <a href="https://sites.google.com/view/gautamsatyndra/blog/plotly-animated-pie-chart">Animated Pie Chart</a>.</p><p>So, instead of going through each element of Plotly figures, the following reusable with only three inputs/params will return an animated figure. It also contains comments to help you understand what is going on. And if anyone wishes to use it, he/she can edit it with minimal adjustments.</p><pre>import pandas as pd <br>import plotly.express as px <br>import plotly.graph_objects as go <br>import humanize <br><br>def create_animated_pie_chart(labels, values, anim_speed=500):<br>    &quot;&quot;&quot;<br>    This python function is designed to create interactive and visually <br>    appealing pie chart. It takes labels, values, and animation speed as <br>    inputs and returns a pie chart with button to play around.<br><br>    :params: labels, metric values, and animation speed <br>    :returns: a pie chart with animation effects <br>    <br>    &quot;&quot;&quot;<br><br>    # check inputs are valid<br>    if len(labels) == 0 or len(values) == 0 or len(labels) != len(values):<br>        raise ValueError(&quot;Labels and values must not be empty and must be of the same length.&quot;)<br><br>    # total sum or avg or anything else depening on use case <br>    total = sum(values)<br>    <br>    # initialize the chart creation <br>    fig = go.Figure(<br>        data=[<br>            go.Pie(<br>                labels=labels,<br>                values=values,<br>            )<br>        ]<br>    )<br>    <br>    &quot;&quot;&quot;_________________________________ CUSTOMIZE THE FIGURES _________________________________&quot;&quot;&quot;<br>    <br>    # slices colors - adjust as per the length of the lables <br>    pie_colors = [<br>        &quot;rgba(68, 58, 89, 1)&quot;,<br>        &quot;rgba(238, 74, 96, 1)&quot;,<br>        &quot;rgba(223, 159, 67, 1)&quot;, <br>        &quot;rgba(214, 212, 212, 1)&quot;, <br>        &quot;rgba(22, 110, 220, 1)&quot;<br>    ]<br><br>    # define pulls with respect to maximum value<br>    max_value = max(values)<br>    max_pull = [0.15 if value == max_value else 0 for value in values]<br><br>    # hover background colors - same as above  <br>    hover_bg_colors = [<br>        &quot;rgba(68, 58, 89, 0.2)&quot;,<br>        &quot;rgba(238, 74, 96, 0.1)&quot;,<br>        &quot;rgba(223, 159, 67, 0.1)&quot;, <br>        &quot;rgba(214, 212, 212, 0.05)&quot;, <br>        &quot;rgba(22, 110, 220, 0.1)&quot;<br>    ]<br><br>    # define hover components <br>    hover_label_colors = [pie_colors[i] for i in range(len(labels))]<br>    hover_values = [v for v in values]<br>    hover_lables = [l for l in labels]<br><br>    # create list comprehension for hover texts per slice <br>    hover_texts = []<br>    for label, value, colors in zip(hover_lables, hover_values, hover_label_colors):<br>        hover_text = (<br>            f&quot;&lt;span&gt;&lt;b&gt;    &lt;/b&gt;&lt;/span&gt;&lt;br&gt;&lt;span&gt;&lt;b&gt; &lt;/b&gt;&lt;/span&gt;&quot;<br>            + f&quot;&lt;br&gt;&lt;span &gt;&lt;b&gt;      &lt;/b&gt;&lt;/span&gt;&lt;span style=&#39;font-size:17.5px; color: {colors};&#39; &gt;&lt;b style=&#39;font-family: lexend;&#39;&gt; Continent: {label}&lt;/b&gt;&lt;/span&gt;&lt;span&gt;&lt;b&gt;      &lt;/b&gt;&lt;/span&gt;&lt;br&gt;&lt;br&gt;&quot;<br>            + f&quot;&lt;span&gt;&lt;b&gt;      &lt;/b&gt;&lt;/span&gt;&lt;span style=&#39;color: rgba(51, 55, 75, 0.85); &#39;&gt;Total Population: &lt;/span&gt;&lt;span style=&#39;color: {colors}; font-size:14.5px; font-weight: bold; font-family: lexend;&#39;&gt;{value:,d}&lt;/span&gt;&lt;span&gt;&lt;b&gt;       &lt;/b&gt;&lt;/span&gt;&lt;br&gt;&lt;br&gt;&quot;<br>            + f&quot;&lt;br&gt;&lt;span&gt;&lt;b&gt;      &lt;/b&gt;&lt;/span&gt;&lt;span&gt;&lt;b&gt; &lt;/b&gt;&lt;/span&gt;&lt;span&gt;&lt;b&gt;        &lt;/b&gt;&lt;/span&gt;&lt;br&gt;&quot;<br>        )<br>        hover_texts.append(hover_text)<br><br>    # custom subtitle using annotations <br>    # max_value_label = labels[values.index(max_value)]<br>    max_value_label = [label for label, value in zip(labels, values) if value == max_value][0]<br>    formatted_max_value = humanize.intword(max_value).replace(&#39;million&#39;, &#39;M&#39;).replace(&#39;billion&#39;, &#39;B&#39;)<br>    formatted_total_value = humanize.intword(total).replace(&#39;million&#39;, &#39;M&#39;).replace(&#39;billion&#39;, &#39;B&#39;)<br><br>    # NOT USISNG IT <br>    subtitle_annotation = [<br>        dict(<br>            text=f&quot;&quot;&quot;Most population in &lt;span style=&#39;font-size: 14px; font-weight: thin;&#39;&gt;{max_value_label}&lt;/span&gt; : &lt;br&gt;&lt;br&gt; &lt;b style=&quot;font-size: 34px;&quot;&gt;{formatted_max_value}&lt;/b&gt;&quot;&quot;&quot;,<br>            x=0.02,<br>            y=0.75,  # Adjust y-value to control the gap<br>            font=dict(<br>                size=16, <br>                family=&quot;Lexend&quot;,<br>            ),<br>            showarrow=False,<br>            xanchor=&#39;right&#39;,<br>            yanchor=&#39;top&#39;,<br>            # xref = &#39;paper&#39;,<br>            # yref = &#39;paper&#39;,<br>        )<br>    ]<br><br>    # legend positioning<br>    legend_pos = dict(<br>        yanchor=&quot;bottom&quot;,<br>        y= -0.215,<br>        xanchor=&quot;center&quot;,<br>        x= 0.53,<br>        orientation=&quot;h&quot;,<br>        entrywidth=50, # used for gapping between legends horizontally<br>        bgcolor=&quot;rgba(0, 0, 0, 0)&quot;,<br>    )<br><br>    # update the figure<br>    fig.update_traces(<br>        marker=dict(<br>            colors = [pie_colors[i] for i in range(len(labels))]<br>        ),<br>        pull=max_pull,<br>        hoverinfo=&quot;skip&quot;,<br>        textposition = &#39;outside&#39;,<br>        textfont = dict(<br>            size=13,<br>            family= &#39;Lexend&#39;<br>        )<br>    )<br><br>    initial_title = f&quot;&quot;&quot;&lt;br&gt;&lt;span style=&#39;font-weight: bold;&#39;&gt;Plotly Pie Chart - Total Population &lt;br&gt;by Continents (2007)&lt;/span&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&quot;&quot;&quot;<br>    initial_subtitle = f&quot;&quot;&quot;&lt;sup&gt;Total population : &lt;br&gt;&lt;br&gt; &lt;b style=&quot;font-size: 34px; color: #205f5b;&quot;&gt;{formatted_total_value}&lt;/b&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;/sup&gt;&quot;&quot;&quot;<br>    initial_bottom_gap = f&quot;&lt;br&gt;&lt;span&gt;&lt;b&gt;      &lt;/b&gt;&lt;/span&gt;&lt;span&gt;&lt;b&gt; &lt;/b&gt;&lt;/span&gt;&lt;span&gt;&lt;b&gt;        &lt;/b&gt;&lt;/span&gt;&lt;br&gt;&quot;<br>    custom_titles = initial_title+initial_subtitle+initial_bottom_gap<br>    <br>    # update the layout<br>    fig.update_layout(<br>        title = custom_titles,<br>        # title=&quot;&lt;br&gt;Plotly Pie Chart - Total Population &lt;br&gt;by Continents (2007)&lt;br&gt;&lt;sup&gt;Use sup&lt;/sup&gt;&quot;,<br>        title_font=dict(<br>            family=&quot;Lexend&quot;,  <br>            size=17,                    <br>            color=&quot;#29075C&quot;,<br>            # weight=&quot;bold&quot;<br>        ),<br>        # annotations=subtitle_annotation,  <br>        autosize=True,<br>        margin=dict(l=20, r=20, t=70, b=40),  <br>        plot_bgcolor=&#39;rgba(0, 0, 0, 0)&#39;,<br>        paper_bgcolor=&#39;rgba(0, 0, 0, 0)&#39;,<br>        showlegend=True,<br>        legend=legend_pos<br>    )<br><br><br>    # create second pie chart and add trace <br>    shadow_trace_slice_colors = [&#39;rgba(0, 0, 0, 0.14)&#39;] * 6<br>    <br>    shadow_trace = go.Pie(<br>        labels=labels,<br>        values=values,<br>        hole=0,<br>        textinfo=&#39;none&#39;,<br>        hoverinfo=&#39;text&#39;,<br>        hovertext=hover_texts,<br>        hoverlabel=dict(<br>            bgcolor=[hover_bg_colors[i] for i in range(len(labels))], <br>            bordercolor=[pie_colors[i] for i in range(len(labels))],<br>        ),<br>        pull=max_pull,<br>        marker=dict(<br>            colors=[shadow_trace_slice_colors[i] for i in range(len(labels))],<br>            line=dict(color=&#39;rgba(255, 255, 255, 0.1)&#39;, width=0.8), <br>        ),<br>        domain=dict(x=[0.05, 0.95], y=[0.05, 0.95]),<br>    )<br><br>    # add the trace<br>    fig.add_trace(shadow_trace)<br><br>    &quot;&quot;&quot;________________________________ START ANIMATION EFFECTS ________________________________&quot;&quot;&quot;<br>    <br>    # list comprehension for frames<br>    frames = []<br>    for i in range(len(labels)):<br>        # define the pulls<br>        pulls = [0.2 if j == i else 0 for j in range(len(labels))]<br>        <br>        # for each frame, update both the main pie and shadow pie trace<br>        frames.append(<br>            go.Frame(<br>                data=[<br>                    # main pie chart <br>                    go.Pie(<br>                        labels=labels,<br>                        values=values,<br>                        pull=pulls<br>                    ),<br>                    # shadow pie chart <br>                    go.Pie(<br>                        labels=labels,<br>                        values=values,<br>                        pull=pulls,  <br>                    )<br>                ], <br>                name=str(i)<br>            )<br>        )<br><br>    # reset to the original state of figure after the loop<br>    existing_pie_data = fig.data<br>    initial_frame = go.Frame(data=[existing_pie_data[0], existing_pie_data[1]], name=&#39;initial&#39;)<br><br>    # capturing the original layout components(text)<br>    original_title = fig.layout.title.text<br>    <br>    # resetting the title to its original state<br>    initial_frame[&#39;layout&#39;].update(title_text=original_title)<br>    frames.append(initial_frame)<br><br>    # update layout to include buttons for controlling the animation<br>    fig.update_layout(<br>        updatemenus=[<br>            {<br>                &#39;type&#39;: &#39;buttons&#39;,<br>                &#39;showactive&#39;: True,<br>                &#39;buttons&#39;: [<br>                    {<br>                        &#39;label&#39;: &#39;      Play      &#39;,<br>                        &#39;method&#39;: &#39;animate&#39;,<br>                        &#39;args&#39;: [<br>                            None, <br>                                {<br>                                &#39;frame&#39;: {<br>                                    &#39;duration&#39;: anim_speed, <br>                                    &#39;redraw&#39;: True<br>                                },<br>                                &#39;fromcurrent&#39;: True,<br>                                &#39;mode&#39;: &#39;immediate&#39;,<br>                                &#39;transition&#39;: {<br>                                    &#39;duration&#39;: 0<br>                                },<br>                            }<br>                        ]<br>                    },<br>                ],<br>                &#39;x&#39;: 0.1,<br>                &#39;xanchor&#39;: &#39;right&#39;,<br>                &#39;y&#39;: -0.06,<br>                &#39;yanchor&#39;: &#39;top&#39;<br>            }<br>        ],<br>    )<br>        <br>    # Add frames to the figure<br>    fig.frames = frames<br><br>    # configure dynamic text/titles <br>    for i, label, value, text_color in zip(range(len(fig.frames)), labels, values, pie_colors):<br>        <br>        # define title and subtitle components <br>        formatted_value = humanize.intword(value).replace(&#39;million&#39;, &#39;M&#39;).replace(&#39;billion&#39;, &#39;B&#39;)<br>        title = f&quot;&quot;&quot;&lt;br&gt;&lt;span style=&#39;font-weight: bold;&#39;&gt;Plotly Pie Chart - Total Population &lt;br&gt;by Continents (2007)&lt;/span&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&quot;&quot;&quot;<br>        subtitle = f&quot;&quot;&quot;&lt;sup&gt;Total population in &lt;span style=&#39;font-size: 17px; font-weight: thin; color: {text_color};&#39;&gt;{label}&lt;/span&gt; : &lt;br&gt;&lt;br&gt; &lt;b style=&quot;font-size: 34px; color: {text_color};&quot;&gt;{formatted_value}&lt;/b&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;/sup&gt;&quot;&quot;&quot;<br>        bottom_gap = f&quot;&lt;br&gt;&lt;span&gt;&lt;b&gt;      &lt;/b&gt;&lt;/span&gt;&lt;span&gt;&lt;b&gt; &lt;/b&gt;&lt;/span&gt;&lt;span&gt;&lt;b&gt;        &lt;/b&gt;&lt;/span&gt;&lt;br&gt;&quot;<br>        custom_titles = title+subtitle+bottom_gap<br>        <br>        fig.frames[i][&#39;layout&#39;].update(title_text=f&#39;{custom_titles}&#39;)<br><br>    return fig </pre><p>Now call the function;</p><pre>&quot;&quot;&quot; CALL THE FUNCTION AND PROVIDE PARAMS &quot;&quot;&quot;<br><br># load the gapminder dataset<br>df = px.data.gapminder()<br><br>df = df[df[&#39;year&#39;] == 2007].groupby(&#39;continent&#39;)[&#39;pop&#39;].sum().reset_index()<br>df = df.sort_values(&#39;pop&#39;, ascending=False).reset_index(drop=True)<br>df.head()<br><br>labels = df[&#39;continent&#39;].unique() # in case, avoid duplicates<br>values = df[&#39;pop&#39;]<br><br># calling the function <br>final_pie_chart = create_animated_pie_chart(labels, values, anim_speed=600)<br>final_pie_chart.show(config={&#39;displayModeBar&#39;: False})</pre><p><em>Result;</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*749PXRhr3cQNOFGVLJgJJQ.png" /><figcaption>Animated Pie Chart</figcaption></figure><p>Voila! We did it!</p><p>Plotly Figure Data is pretty strong. It can be re-used to manipulate charts whenever needed. You don’t have to actually create the figure from scratch if already created. You just use<em> ‘</em><strong><em>fig.data’</em></strong><em> </em>and carefully update your existing figure’s components.<em> </em>This is why Plotly Graph Objects is powerful for developing interactive and informative plots and for more sophisticated customizations.</p><p>The more you play around with your charts, the more you learn. If you wish to see this chart in action on hover using Dash — a framework used to create web applications, you can use the following script incorporating the same stuff as above.</p><pre>import dash<br>from dash import dcc, html<br>from dash.dependencies import Input, Output<br>import plotly.graph_objects as go<br>import plotly.express as px <br><br># define app object <br>app = dash.Dash(__name__)<br>server = app.server<br><br># retrieve data<br>df = px.data.gapminder()<br>df = df[df[&#39;year&#39;] == 2007].groupby(&#39;continent&#39;)[&#39;pop&#39;].sum().reset_index()<br>df = df.sort_values(&#39;pop&#39;, ascending=False).reset_index(drop=True)<br><br># extract labels and hover <br>labels = df[&#39;continent&#39;].unique() # in case, avoid duplicates<br>values = df[&#39;pop&#39;] <br><br># define the Dash app layout<br>app.layout = html.Div([<br>    dcc.Graph(<br>        id=&#39;donut-chart&#39;,<br>        config={&#39;editable&#39;: True, &#39;displayModeBar&#39;: False},<br>    )<br>])<br><br># callback to update pulls<br>@app.callback(<br>    Output(&#39;donut-chart&#39;, &#39;figure&#39;),<br>    Input(&#39;donut-chart&#39;, &#39;hoverData&#39;),<br>)<br>def update_pie_chart_on_hover(hover_data):<br>    <br>    # initial pull values <br>    initial_pull = 0 # if needed <br>    max_value = max(values)<br><br>    # default pulled-out slice for maximum value<br>    max_pull = [0.1 if value == max_value else 0 for value in values]<br><br>    # pulls on hover<br>    pull_hover = 0.2<br><br>    # define colors <br>    pie_colors = [<br>        &quot;rgba(68, 58, 89, 1)&quot;,<br>        &quot;rgba(238, 74, 96, 1)&quot;,<br>        &quot;rgba(223, 159, 67, 1)&quot;, <br>        &quot;rgba(214, 212, 212, 1)&quot;, <br>        &quot;rgba(22, 110, 220, 1)&quot;<br>    ]<br><br>    # legend positioning<br>    legend_pos = dict(<br>        yanchor=&quot;bottom&quot;,<br>        y= -0.215,<br>        xanchor=&quot;center&quot;,<br>        x= 0.53,<br>        orientation=&quot;h&quot;,<br>        entrywidth=50, # used for gapping between legends horizontally<br>        bgcolor=&quot;rgba(0, 0, 0, 0)&quot;,<br>    )<br>    <br>    # initial donut chart with colors for initial slices<br>    pie_chart = go.Figure(<br>        go.Pie(<br>            labels=labels,<br>            values=values,<br>            pull = max_pull,<br>            textinfo=&#39;value&#39;,<br>            textposition=&#39;outside&#39;,<br>            textfont=dict(<br>                color=[pie_colors[i] for i in range(len(labels))] <br>            ), <br>            marker=dict(<br>                colors=[pie_colors[i] for i in range(len(labels))] <br>            )  <br>        )<br>    )<br><br>    # adjust layout<br>    pie_chart.update_layout(<br>        margin=dict(l=20, r=20, t=50, b=37),<br>        paper_bgcolor=&#39;rgba(0,0,0,0)&#39;,<br>        plot_bgcolor=&#39;rgba(0,0,0,0)&#39;,<br>        title = &quot;Pie chart with Plotly Dash&quot;,<br>        legend = legend_pos, <br>        autosize = True,<br>    )<br>    <br>    <br>    # colors for shadow trace<br>    hover_colors = [&#39;rgba(0, 0, 0, 0.15)&#39;] * 5<br>    <br>    <br>    if hover_data:<br>        index = hover_data[&#39;points&#39;][0][&#39;pointNumber&#39;]<br>    else:<br>        index = None<br><br>    if index is not None:<br>        # new pull<br>        new_pull = [pull_hover if i == index else 0 for i in range(len(labels))]<br><br>        # shadow trace<br>        shadow_trace = go.Pie(<br>            labels=labels,<br>            values=values,<br>            textinfo=&#39;none&#39;,<br>            pull=new_pull,<br>            textposition = &#39;outside&#39;,<br>            marker = dict(<br>                colors=[hover_colors[i] for i in range(len(labels))],<br>                line=dict(color=&#39;rgba(255, 255, 255, 0.1)&#39;, width=0.8), <br>            ),<br>            domain = dict(  # adjust domain for slight shift for shadow effect<br>                x=[0.04, 0.96], <br>                y=[0.04, 0.96]<br>            ) <br>        )<br><br>        # update it with new pull<br>        pie_chart.update_traces(pull=new_pull)<br><br>        # add shadow trace <br>        pie_chart.add_trace(shadow_trace)<br>    else:<br>        None<br><br>    # return the figure <br>    return pie_chart<br><br><br>if __name__ == &#39;__main__&#39;:<br>    app.run_server(debug=True)</pre><p>I hope you enjoyed reading this post. Since this is my first post and I would love to grab your attention for your feedback and any advice that would help me improve continuously.</p><p>Thank you so much!</p><h3>Resources</h3><p>● <a href="https://plotly.com/">Plotly</a></p><p>● <a href="https://stackoverflow.com/">StackOverflow</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=06513b1318c8" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>