Skip to content

Commit

Permalink
refactor select subregions to function
Browse files Browse the repository at this point in the history
  • Loading branch information
joAschauer committed Nov 23, 2023
1 parent 47f5970 commit 2dbd0a6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
8 changes: 6 additions & 2 deletions app/plot_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
import plotly.graph_objects as go
import streamlit as st

from app.ptxboa_functions import remove_subregions, subset_and_pivot_input_data
from app.ptxboa_functions import (
remove_subregions,
select_subregions,
subset_and_pivot_input_data,
)
from ptxboa.api import PtxboaAPI


Expand Down Expand Up @@ -226,7 +230,7 @@ def _choropleth_map_deep_dive_country(
if custom_data_func_kwargs is None:
custom_data_func_kwargs = {}
# subsetting 'df' for the selected deep dive country
df = df.copy().loc[df.index.str.startswith(f"{deep_dive_country} ("), :]
df = select_subregions(df, deep_dive_country)
# need to calculate custom data befor is03166 column is appended.
hover_data = custom_data_func(df, **custom_data_func_kwargs)
# get dataframe with info about iso 3166-2 codes and map them to res_costs
Expand Down
22 changes: 22 additions & 0 deletions app/ptxboa_functions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
"""Utility functions for streamlit app."""
from typing import Literal

import pandas as pd
import streamlit as st
Expand Down Expand Up @@ -190,6 +191,27 @@ def remove_subregions(api: PtxboaAPI, df: pd.DataFrame, country_name: str):
return df


def select_subregions(
df: pd.DataFrame, deep_dive_country: Literal["Argentina", "Morocco", "South Africa"]
) -> pd.DataFrame:
"""
Only select rows corresponding to subregions of a deep dive country.
Parameters
----------
df : pd.DataFrame
pandas DataFrame with list of regions as index.
deep_dive_country : str in {"Argentina", "Morocco", "South Africa"}
Returns
-------
pd.DataFrame
"""
df = df.copy().loc[df.index.str.startswith(f"{deep_dive_country} ("), :]
return df


def reset_user_changes():
"""Reset all user changes."""
if (
Expand Down

0 comments on commit 2dbd0a6

Please sign in to comment.