2024 • 4 min read

Visualizing 24 Years of Food Trade
 Vulnerabilities Across 18 Pacific Island Nations

How I optimized Streamlit and Plotly to explore food import dependencies across 18 Pacific island countries with pre-aggregated trade matrices.

Visualizing 24 Years of Food Trade Vulnerabilities Across 18 Pacific Island Nations

The Fragile Island Supply Line

Pacific island nations face a quiet supply chain fragility. Scattered across millions of square kilometers of ocean, small island states rely heavily on maritime shipping corridors for daily food supplies. If a single exporting nation experiences a prolonged drought or a commercial shipping carrier changes its freight routes, an entire island can face immediate staple food shortages.

For a Data Visualization course at Telkom University, I wanted to turn historical trade archives into an interactive analytical tool.

Using the Pacific Food Trade Database (PFTD 2.1 - HS92), which spans twenty-four years of transaction records (1995 to 2018), I built an exploratory dashboard in Python, Streamlit, and Plotly to investigate supplier concentration risks across 18 Pacific island nations.

65 MB Raw PFTD 2.1 Dataset (1995-2018)
                 │
                 ▼
        The Performance Trap
   (Running on-the-fly Pandas groupbys
    on every filter change froze Streamlit)
                 │
                 ▼
     Pre-Aggregation at Startup
   ├── Shape 1: (Importer, Commodity, Year) -> Total Volume
   └── Shape 2: (Importer, Exporter, Commodity, Year) -> Bilateral Flow
                 │
                 ▼
      Sub-100ms Reactive Dashboard
   ├── Plotly Treemaps (Supplier Concentration Share)
   ├── Pacific Choropleth Maps (Regional Dependency Ratios)
   └── Time Series Trends (1995-2018 Structural Shifts)

The Single-Supplier Reality

Analyzing the database revealed striking dependency figures:

  • Multiple island nations source more than 70% of their staple rice imports from a single supplier country.
  • Dairy and processed wheat dependencies are heavily concentrated in Australia and New Zealand.
  • Across several commodity categories, these concentration ratios barely diversified between 1995 and 2018, showing structural dependencies that persisted over two decades.

When a country imports 80% of its grain from one trading partner, any port strike, harvest failure, or export policy shift in that single partner nation becomes an immediate domestic crisis.

The Performance Bottleneck: On-the-Fly Aggregations

The underlying dataset was a 65 MB CSV containing hundreds of thousands of disaggregated shipment logs classified under the Harmonized System (HS92) standard.

In our first dashboard prototype, we hooked Streamlit filter widgets directly to raw Pandas queries:

# Naive approach: filtering and grouping raw rows on every widget interaction
@st.cache_data
def get_filtered_data(year, commodity, importer):
    df_filtered = raw_df[
        (raw_df['year'] == year) &
        (raw_df['commodity'] == commodity) &
        (raw_df['importer'] == importer)
    ]
    return df_filtered.groupby('exporter')['volume'].sum().reset_index()

Every time a user dragged the year slider from 2004 to 2005 or selected a new commodity from the dropdown, Streamlit recalculated multi-index groupbys on hundreds of thousands of rows. The browser froze for three to four seconds, and moving between years felt sluggish and frustrating.

How do we make slicing 24 years of multi-country trade records feel instantaneous without freezing the browser tab?

Pre-Aggregated Query Shapes

To achieve real-time responsiveness, I separated raw data ingestion from interactive filtering.

Instead of computing sums during user clicks, the application pre-calculates the most common analytical matrices at launch and stores them in memory:

# Pre-calculating core query shapes during application startup
@st.cache_resource
def load_and_preaggregate():
    raw_df = pd.read_csv("pftd_2_1.csv")

    # Shape 1: Total commodity imports per nation per year
    total_imports = raw_df.groupby(
        ['importer_country', 'commodity_group', 'year']
    )['trade_value_usd'].sum().rename('total_import_value')

    # Shape 2: Bilateral trade flows
    bilateral_flows = raw_df.groupby(
        ['importer_country', 'exporter_country', 'commodity_group', 'year']
    )['trade_value_usd'].sum().reset_index()

    # Calculate dependency ratio directly on aggregated matrices
    merged = bilateral_flows.merge(
        total_imports, 
        on=['importer_country', 'commodity_group', 'year']
    )
    merged['dependency_ratio'] = merged['trade_value_usd'] / merged['total_import_value']

    return merged

By pre-aggregating the data into indexed summary tables, UI interactions no longer touch raw transaction rows. Dragging the year slider or selecting a new island nation performs simple index lookups on compact dataframes.

Response latency dropped from over three seconds to under 80 milliseconds.

Interactive Visual Hierarchies

With the data engine responding in real time, I designed three coordinated visual views in Plotly:

  1. Choropleth Dependency Map: Visualizes geopolitical risk across the Pacific basin, color-coding island nations where a single exporter accounts for more than 70% of a staple commodity’s imports.
  2. Exporter Treemaps: Displays hierarchical boxes of supplier shares for any selected country, making market domination visible at a glance.
  3. Decade-Spanning Time Series: Plots bilateral volume trajectories from 1995 through 2018, showing whether an island has diversified its supply partners over time or remained locked into single-supplier routes.

What I Learned

This project showed me that data visualization is fundamentally about interaction speed.

If a dashboard takes three seconds to update every time a user changes a filter, they stop exploring and stick to surface-level charts. Taking the time to pre-aggregate multi-dimensional matrices turned an unwieldy 65 MB dataset into a responsive exploration tool.

Transforming raw shipping manifests into intuitive visual ratios helped communicate a critical macroeconomic reality: for small island states, supply chain resilience is not just an abstract trade statistic, but a foundation of food security.