Skip to content

Commit

Permalink
Update the Binder env and example notebook (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
mwouts authored Sep 28, 2024
1 parent adc2395 commit 56144e3
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 1,841 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
------------------

**Fixed**
- We now use HTTPS as required by the world bank API ([#18](https://github.com/mwouts/world_bank_data/pulls/18)) - thanks to [John Cant](https://github.com/johncant) for fixing this!
- We now use HTTPS as required by the world bank API ([#18](https://github.com/mwouts/world_bank_data/pull/18)) - thanks to [John Cant](https://github.com/johncant) for fixing this!

**Added**
- Added `__version__`
Expand Down
10 changes: 3 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,9 @@

[![CI](https://github.com/mwouts/world_bank_data/actions/workflows/continuous-integration.yml/badge.svg?branch=main)](https://github.com/mwouts/world_bank_data/actions)
[![codecov.io](https://codecov.io/github/mwouts/world_bank_data/coverage.svg?branch=main)](https://codecov.io/github/mwouts/world_bank_data?branch=main)
[![Language grade: Python](https://img.shields.io/badge/lgtm-A+-brightgreen.svg)](https://lgtm.com/projects/g/mwouts/world_bank_data/context:python)
[![Pypi](https://img.shields.io/pypi/v/world_bank_data.svg)](https://pypi.python.org/pypi/world_bank_data)
[![pyversions](https://img.shields.io/pypi/pyversions/world_bank_data.svg)](https://pypi.python.org/pypi/world_bank_data)
[![Jupyter Notebook](https://img.shields.io/badge/Binder-Notebook-blue.svg)](
https://mybinder.org/v2/gh/mwouts/world_bank_data/main?filepath=examples%2FA%20sunburst%20plot%20of%20the%20world%20population.ipynb)
[![JupyterLab](https://img.shields.io/badge/Binder-JupyterLab-blue.svg)](
https://mybinder.org/v2/gh/mwouts/world_bank_data/main?urlpath=lab)
[![JupyterLab](https://img.shields.io/badge/Binder-JupyterLab-blue.svg)](https://mybinder.org/v2/gh/mwouts/world_bank_data/main?urlpath=lab)

This is an implementation of the [World Bank API v2](https://datahelpdesk.worldbank.org/knowledgebase/articles/889386-developer-information-overview) in Python. Use this package to explore the [World Development Indicators](http://datatopics.worldbank.org/world-development-indicators/) published by the [World Bank](http://www.worldbank.org/).

Expand Down Expand Up @@ -94,9 +90,9 @@ wb.get_series('SP.POP.TOTL', date='2016', id_or_value='id', simplify_index=True)

## Ready for an interative tutorial?

Go to our [Binder](https://mybinder.org/v2/gh/mwouts/world_bank_data/main) and run either this [README](https://github.com/mwouts/world_bank_data/blob/main/README.md), or our other [tutorial](https://github.com/mwouts/world_bank_data/blob/main/examples/) with the code required to produce this plot of the World Population:
Go to our Binder and run either this [README](https://mybinder.org/v2/gh/mwouts/world_bank_data/main?labpath=README.md) or the notebook under `examples` to reproduce produce this plot of the World Population:

[![World Population 2017](https://gist.githubusercontent.com/mwouts/ec3a88f1d97e36a062f69d4072b91e39/raw/79211a09957c6934fabf738a59c2c9b8df943696/world_population.gif)](https://nbviewer.jupyter.org/github/mwouts/world_bank_data/blob/main/examples/A%20sunburst%20plot%20of%20the%20world%20population.ipynb)
[![World Population 2017](https://gist.githubusercontent.com/mwouts/ec3a88f1d97e36a062f69d4072b91e39/raw/79211a09957c6934fabf738a59c2c9b8df943696/world_population.gif)](https://mybinder.org/v2/gh/mwouts/world_bank_data/main?labpath=examples/A%20sunburst%20plot%20of%20the%20world%20population.py)

# References

Expand Down
13 changes: 13 additions & 0 deletions binder/labconfig/default_setting_overrides.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"@jupyterlab/docmanager-extension:plugin": {
"defaultViewers": {
"markdown": "Jupytext Notebook",
"myst": "Jupytext Notebook",
"r-markdown": "Jupytext Notebook",
"quarto": "Jupytext Notebook",
"julia": "Jupytext Notebook",
"python": "Jupytext Notebook",
"r": "Jupytext Notebook"
}
}
}
20 changes: 13 additions & 7 deletions binder/postBuild
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
#!/bin/bash
set -ex
# Stop everything if one command fails
set -e

# Trust our notebooks
jupyter trust examples/*.ipynb
# Install from source
pip install .

# TOC extension
jupyter contrib nbextension install --user
jupyter nbextension enable toc2/main
# Open text notebooks with the Notebook editor
mkdir -p ${HOME}/.jupyter/labconfig
cp binder/labconfig/* ${HOME}/.jupyter/labconfig

# Install a kernel called world_bank_data
python -m ipykernel install --name world_bank_data --user

# Use that kernel for the README and the example notebooks
jupytext README.md examples/* --set-kernel world_bank_data
7 changes: 4 additions & 3 deletions binder/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
world_bank_data
plotly>=3.8
itables
plotly
jupytext
jupyter_contrib_nbextensions
jupyterlab_execute_time
jupyter-resource-usage
1,792 changes: 0 additions & 1,792 deletions examples/A sunburst plot of the world population.ipynb

This file was deleted.

53 changes: 22 additions & 31 deletions examples/A sunburst plot of the world population.py
Original file line number Diff line number Diff line change
@@ -1,55 +1,49 @@
# ---
# jupyter:
# jupytext:
# formats: ipynb,py:light
# formats: ipynb,py:percent
# text_representation:
# extension: .py
# format_name: light
# format_version: '1.4'
# jupytext_version: 1.2.0-rc1
# format_name: percent
# format_version: '1.3'
# jupytext_version: 1.16.1
# kernelspec:
# display_name: Python 3
# display_name: Python 3 (ipykernel)
# language: python
# name: python3
# ---

# %% [markdown]
# In this notebook we use the new Sunburst plot by [plotly](http://plot.ly/) to illustrate how the World population
# is splitted among regions and countries. The data set illustrated here originates from the
# [World Bank](https://data.worldbank.org). This notebook is also a quick demo for the
# [world_bank_data](https://github.com/mwouts/world_bank_data/blob/main/README.md) Python package.

# +
# %%
import pandas as pd
import plotly
import plotly.offline as offline
import plotly.graph_objects as go
from itables import init_notebook_mode

import world_bank_data as wb

init_notebook_mode(all_interactive=True)

def version_to_int_list(version):
return [int(s) for s in version.split(".")]


assert version_to_int_list(plotly.__version__) >= version_to_int_list(
"3.8.0"
), "Sunburst plots require Plotly >= 3.8.0"

pd.set_option("display.max_rows", 12)
offline.init_notebook_mode()
# -

# %%
# Countries and associated regions
countries = wb.get_countries()
countries

# %%
# Population dataset, by the World Bank (most recent value)
population = wb.get_series("SP.POP.TOTL", mrv=1)
population

# %%
# Same data set, indexed with the country code
population = wb.get_series("SP.POP.TOTL", id_or_value="id", simplify_index=True, mrv=1)
population

# %%
# Aggregate region, country and population
df = (
countries[["region", "name"]]
Expand All @@ -59,7 +53,7 @@ def version_to_int_list(version):
df["population"] = population
df

# +
# %%
# The sunburst plot requires weights (values), labels, and parent (region, or World)
# We build the corresponding table here
columns = ["parents", "labels", "values"]
Expand Down Expand Up @@ -90,17 +84,14 @@ def version_to_int_list(version):

all_levels = pd.concat([level1, level2, level3], axis=0).reset_index(drop=True)
all_levels
# -

# %%
# And now we can plot the World Population
offline.iplot(
dict(
data=[dict(type="sunburst", hoverinfo="text", **all_levels)],
layout=dict(
title="World Population (World Bank, 2017)<br>Click on a region to zoom",
width=800,
height=800,
),
go.Figure(
data=[go.Sunburst(hoverinfo="text", **all_levels)],
layout=dict(
title="World Population (World Bank, 2017)<br>Click on a region to zoom",
width=800,
height=800,
),
validate=False,
)

0 comments on commit 56144e3

Please sign in to comment.