Insanely Easy GPT-4 One Prompt Streamlit Interactive Plotly Sunburst Chart
Simple and modular prompt engineering for efficient Python charting results
GPT-4 is terrific for generating data cleaning and visualization code in Python.
In combination with the right libraries — for example Streamlit and Plotly — you can create pleasing and interactive data visualizations.
Today, I want to share with you a visually stunning and interactive display that showcases a multi-level sunburst chart — all created with a single prompt to GPT-4.
A sunburst chart allows us to display hierarchical multi-level data using concentric circles.
I have a terrific data set to demonstrate how sunburst charts can provide multi-level data analysis — the UN High Commission for Refugees dataset.
Starting with the data set, let’s use a modular step-by-step approach to create our dashboard with a single GPT-4 prompt.
The Data Set
The UN High Commission for Refugees (UNHCR) tracks statistics on refugee movements across the globe.
Their data is freely accessible HERE.
After clicking the link to get to the download page, we can be granular on the data that we select:
For this project, let’s retrieve the county of origin for each refugee and the country of asylum.
This is a perfect dataset to demonstrate the power of GPT-4 for Streamlit/Plotly code creation.
With this data, we can create global maps that show refugee data:
from country of origin — where asylum seekers are going to
from country of asylum — where asylum seekers are coming from
Once we download the dataset, we can open it up in spreadsheet format to see what we are dealing with:
The data fields that we are interested in for this project are:
Country of origin (including 3-letter ISO code) — where a person seeking asylum is coming from
Country of asylum (including 3-letter ISO code) — where a person is actually seeking asylum
Recognized decisions — whether the person seeking asylum was accepted (numeric total by country)
Both the country of origin and asylum have a 3-letter ISO code that can be used for creating choropleth maps.
This is super useful! It greatly simplifies map creation.
Now on to the GPT-4 prompt!
Prompting for a Sunburst Chart Dashboard
To set up our interactive sunburst chart, we want to first allow the user to select a country (from a dropdown) as the origin country. The chart must then display each year with all of the countries where folks from this country are seeking asylum to.
These days, the best option for creating sunburst charts in Python is using the Plotly library’s sunburst() function
Why this function? Plotly sunburst charts are highly interactive and customizable. They allow users to drill down into data by clicking on segments of the inner circles to reveal more detailed information in the outer rings. They also provide customization options, including color schemes, labels, and themes.
To start our prompt, we need to make sure that we upload our CSV file to the GPT-4 chat window (click on the attachment icon, find your file, click “Upload”):
Once our file is accessible, we need to specify 4 very specific details for our prompt.
Prompt to GPT-4: I want you to create the Python code for a Streamlit Plotly dashboard using the provided dataset. The dashboard must:
Allow the user to select a country of origin from a dropdown.
Below the dropdown, display a sunburst chart that shows the asylum countries for that selected country of origin based on recognized decisions for each year.
The dashboard must have a height of 600px
This is the only prompt you need. After uploading the file to GPT-4, you can literally copy/paste the above prompt into the GPT-4 chat window.
From this prompt, GPT-4 will generate working Python code:
import streamlit as st
import pandas as pd
import plotly.express as px
# Load the dataset
file_path = 'asylum-decisions.csv'
df = pd.read_csv(file_path)
# Streamlit app
st.title("Asylum Decisions Dashboard")
# Dropdown menu for selecting a country of origin
countries_of_origin = df['Country of origin'].unique()
selected_country = st.selectbox("Select a country of origin", countries_of_origin)
# Filter data based on the selected country
filtered_df = df[df['Country of origin'] == selected_country]
# Create the sunburst chart
fig = px.sunburst(
filtered_df,
path=['Year', 'Country of asylum'],
values='Recognized decisions',
title=f"Recognized Asylum Decisions for {selected_country}",
height=600
)
# Display the chart
st.plotly_chart(fig)
Here’s a summary of the code for this simple Streamlit dashboard:
Load the dataset: The dataset is loaded from the specified path (using the pandas read_csv() function
Streamlit app: The Streamlit app is set up with a title (you can also specify st.ubheader() here instead for a smaller title).
Dropdown menu: A dropdown menu is created to allow the user to select a country of origin (using unique() so there are no duplicates).
Filter data: The data is filtered based on the selected_country of origin (using the value selected in the dropdown).
Create the sunburst chart: A sunburst chart is created using the built-in Plotly Express sunburst() function, showing recognized asylum decisions for each year in different asylum countries.
Display the chart: The chart is displayed in the Streamlit app (using st.plotly_chart() )with the specified height.
To run this code:
Save it in a Python file, e.g.,
streamlit_sunburst01.py
.Ensure you have Streamlit and Plotly installed (
pip install streamlit plotly
).Run the Streamlit app using the command (for example,
streamlit run streamlit_sunburst01.py).
You can Copy/Paste/Save/Run this code into your favourite Python editor (ie. Jupyter Notebooks, PyCharm).
For example, to run Streamlit in PyCharm, we can open up a Terminal window (Click on View/Tool Windows/Terminal), and then type in “streamlit run [file_name]”:
The Streamlit application from the Python file is loaded into your default browser at the next available port (after 8500).
NOTE: As with all current LLMs, GPT-4o (current version) gives variable results. In my experience, the code generated each time was not exactly the same. When I tested this prompt, I received three different versions of code (all working code) with slight variations in the look/feel of the output.
Assuming the path to the CSV file is correct (ie. in the same folder as the Python file), this code will run. The beautiful results in your browser:
One of the terrific interactive functions of the Plotly sunburst chart is the ability to click on a value in the inner circle to drill down to the next level of detail.
For example, if you click on the year 2017 (shown in red in the above image) you can view just the results for that year:
And if you hover over a particular country, you get even more data for that country of asylum (ie. the actual number of recognized decisions for the given year). This is a terrific interactive feature that is part of most Plotly charts.
And that’s all there is to it! Give it a go, leave a comment, let me how it works out for you.
In Summary…
Sunburst charts can provide stunning visualizations of multi-level data sets.
With GPT-4 we can use a single prompt to create an interactive Streamlit dashboard with a dropdown for selecting a country of origin that displays a sunburst chart using the Plotly express library. This sunburst chart shows recognized asylum decisions by year and country of asylum.
I tested this prompt multiple times, each generating a working version of the Streamlit/Plotly code.
The prompting methodology introduced here is a terrific way to rapidly prototype data visualizations when you are in the exploratory phase of data analysis.
A useful next step for this prototype would be to allow the user to click on the year to view a global Choropleth map representation. Keep your eyes peeled for a future article that will demonstrate this functionality!
Until then, thank you for reading.
If you want to learn more about how to prompt engineer GPT-4, sign up for this free 5-day email course on Prompting GPT-4 for data visuals. Let me know what you think:
Thank you for sharing this insightful article on leveraging GPT-4 for creating interactive data visualizations with Streamlit and Plotly Express. The ability to generate a functional dashboard with a single prompt is truly impressive and highlights the efficiency of using advanced language models for rapid prototyping during the exploratory phases of data analysis.
Your methodology for prompting and the successful iterations of the Streamlit/Plotly code demonstrate significant potential for streamlining the development of interactive visualizations. The proposed enhancement to include a clickable year for a global Choropleth map representation sounds like a valuable addition, offering deeper insights and more dynamic user interaction.
I look forward to your future articles and further advancements in this space. Thank you for contributing such practical and forward-thinking solutions to the data visualization community.
Nice write-up. However, looking at generated code and promt, it seems that it could take approximately the same amount of time to write it, especially when using Github Copilot (or another code auto-generator).