Skip to content

Commit

Permalink
Merge pull request #237 from anth-volk/fix/conditional-imports
Browse files Browse the repository at this point in the history
Add conditional imports for heavier packages
  • Loading branch information
MaxGhenis authored Oct 24, 2024
2 parents 0db2769 + b29e752 commit 1419cf5
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 15 deletions.
11 changes: 8 additions & 3 deletions microdf/chart_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import matplotlib as mpl


def dollar_format(suffix=""):
"""Dollar formatter for matplotlib.
Expand All @@ -19,6 +16,14 @@ def currency_format(currency="USD", suffix=""):
:returns: FuncFormatter.
"""
try:
import matplotlib as mpl
except ImportError:
raise ImportError(
"The function you've called requires extra dependencies. " +
"Please install microdf with the 'charts' extra by running " +
"'pip install microdf[charts]'"
)

prefix = {"USD": "$", "GBP": "£"}[currency]

Expand Down
14 changes: 11 additions & 3 deletions microdf/charts.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns

import microdf as mdf

Expand All @@ -19,6 +16,17 @@ def quantile_pct_chg_plot(df1, df2, col1, col2, w1=None, w2=None, q=None):
:returns: Axis.
"""
try:
import seaborn as sns
import matplotlib as mpl
import matplotlib.pyplot as plt
except ImportError:
raise ImportError(
"The function you've called requires extra dependencies. " +
"Please install microdf with the 'charts' extra by running " +
"'pip install microdf[charts]'"
)

if q is None:
q = np.arange(0.1, 1, 0.1)
# Calculate weighted quantiles.
Expand Down
15 changes: 10 additions & 5 deletions microdf/style.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
import matplotlib as mpl
import matplotlib.font_manager as fm
import seaborn as sns


TITLE_COLOR = "#212121"
AXIS_COLOR = "#757575"
GRID_COLOR = "#eeeeee" # Previously lighter #f5f5f5.
Expand All @@ -16,6 +11,16 @@ def set_plot_style(dpi: int = DPI):
(200).
:type dpi: int, optional
"""
try:
import seaborn as sns
import matplotlib as mpl
import matplotlib.font_manager as fm
except ImportError:
raise ImportError(
"The function you've called requires extra dependencies. " +
"Please install microdf with the 'charts' extra by running " +
"'pip install microdf[charts]'"
)

sns.set_style("white")

Expand Down
12 changes: 8 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@
license="MIT",
packages=["microdf"],
install_requires=[
"matplotlib",
"matplotlib-label-lines",
"numpy",
"pandas",
"seaborn",
],
extras_require={"taxcalc": ["taxcalc"]},
extras_require={
"taxcalc": ["taxcalc"],
"charts": [
"seaborn",
"matplotlib",
"matplotlib-label-lines"
]
},
zip_safe=False,
)

0 comments on commit 1419cf5

Please sign in to comment.