An improved, modular rebuild of a "war impact on trade" visualizer. It models global trade flows and geopolitical conflicts as two linked network graphs, computes real graph metrics (centrality, chokepoints, disruption impact), and renders everything as an interactive Streamlit + Plotly dashboard.
This project was built as a direct improvement on a single-file prototype, addressing its main weaknesses:
| Prototype problem | Fix in this version |
|---|---|
One 1700-line app.py |
Split into data/, graph/, viz/, app.py |
| Real dataset (Comtrade CSV) present but never loaded | Data loader reads and blends it with sourced conflict data |
| Conflict list had no sources/dates | Every conflict edge has source + last_updated fields |
| Trade edges were binary (route exists / doesn't) | Edges carry volume_usd_millions so impact is weighted, not binary |
| No real graph analysis | NetworkX centrality, shortest-path rerouting, chokepoint detection |
| No tests | tests/ with pytest covering the graph-building logic |
Committed __pycache__, logs |
.gitignore added |
| No README | This file |
world-trade-network/
├── app.py # Streamlit entry point (UI + layout only)
├── requirements.txt
├── .gitignore
├── README.md
├── data/
│ ├── conflicts.py # Sourced tension/conflict edges (dataclasses)
│ ├── trade_edges.csv # Sample trade-flow dataset (commodity, mode, $ volume)
│ ├── coords_fallback.py # Hardcoded lat/lon fallback if API is unreachable
│ └── loader.py # Loads + validates + merges all data sources
├── graph/
│ ├── build.py # Builds NetworkX graphs + adjacency/impact tensors
│ └── metrics.py # Centrality, chokepoints, rerouting logic
├── viz/
│ └── map.py # Plotly Scattergeo figure builders
└── tests/
└── test_graph.py # pytest unit tests for graph/build.py & metrics.py
# 1. Clone / unzip the project, then enter the folder
cd world-trade-network
# 2. Create and activate a virtual environment
python -m venv venv
source venv/bin/activate # Windows: venv\Scripts\activate
# 3. Install dependencies
pip install -r requirements.txt
# 4. Run the app
streamlit run app.pyThen open the URL Streamlit prints (usually http://localhost:8501).
No API key is required. The app tries a free restcountries.com lookup for
live coordinates and falls back to data/coords_fallback.py automatically if
the network call fails, so it also works fully offline.
To run the test suite:
pip install pytest
pytest tests/ -v- Weighted impact, not binary flags. Every trade edge carries an actual
volume_usd_millionsestimate, so the dashboard can show how much trade value sits behind a conflict-exposed route, not just whether one exists. - Sourced conflict data. Each conflict edge cites a
sourcestring andlast_updateddate so the data is auditable instead of asserted. - Real graph metrics via NetworkX:
- Betweenness centrality — which countries are the most critical trade hubs (removing them fragments the most routes).
- Chokepoint detection — edges whose removal disconnects otherwise linked trade clusters.
- Alternate-route suggestion — if a route is war-exposed, the app computes the next-shortest available path avoiding conflict nodes.
- Modular code — each concern (data, graph math, visualization, UI) is its own file, independently testable and independently readable.
- Tests —
graph/build.pyandgraph/metrics.pyare pure functions with pytest coverage, so future changes can't silently break the math.
- Swap
data/trade_edges.csvfor a live pull of the full UN Comtrade bulk API (requires free registration) to replace the illustrative sample values with real bilateral trade statistics. - Add a time slider once you have multiple years of trade + conflict data, to animate how the network shifts year over year.
- Add
st.download_buttonexports (filtered CSV, PNG snapshot) for reporting. - Deploy to Streamlit Community Cloud for a shareable public demo link.
data/trade_edges.csvis an illustrative sample shaped like real UN Comtrade categories (commodity, mode, reporter, partner, trade value) — swap it for a genuine Comtrade export before drawing real-world conclusions.data/conflicts.pyentries are sourced from public reporting as of thelast_updateddate on each entry; conflict status is fluid and should be re-verified before use in any serious analysis.