1111
1212import markdown as md
1313
14- from esparto import _INSTALLED_MODULES
14+ from esparto import _OptionalDependencies
1515from esparto ._options import options
1616from esparto .design .base import AbstractContent , AbstractLayout , Child
1717from esparto .design .layout import Row
1818from esparto .publish .output import nb_display
1919
20- if " PIL" in _INSTALLED_MODULES :
20+ if _OptionalDependencies . PIL :
2121 from PIL .Image import Image as PILImage # type: ignore
2222
23- if " pandas" in _INSTALLED_MODULES :
23+ if _OptionalDependencies . pandas :
2424 from pandas import DataFrame # type: ignore
2525
26- if " matplotlib" in _INSTALLED_MODULES :
26+ if _OptionalDependencies . matplotlib :
2727 from matplotlib .figure import Figure as MplFigure # type: ignore
2828
29- if " bokeh" in _INSTALLED_MODULES :
29+ if _OptionalDependencies . bokeh :
3030 from bokeh .embed import components # type: ignore
3131 from bokeh .models .layouts import LayoutDOM as BokehObject # type: ignore
3232
33- if " plotly" in _INSTALLED_MODULES :
33+ if _OptionalDependencies . plotly :
3434 from plotly .graph_objs ._figure import Figure as PlotlyFigure # type: ignore
3535 from plotly .io import to_html as plotly_to_html # type: ignore
3636
@@ -103,7 +103,6 @@ class RawHTML(Content):
103103 content : str
104104
105105 def __init__ (self , html : str ) -> None :
106-
107106 if not isinstance (html , str ):
108107 raise TypeError (r"HTML must be str" )
109108
@@ -124,7 +123,6 @@ class Markdown(Content):
124123 _dependencies = {"bootstrap" }
125124
126125 def __init__ (self , text : str ) -> None :
127-
128126 if not isinstance (text , str ):
129127 raise TypeError (r"text must be str" )
130128
@@ -163,10 +161,9 @@ def __init__(
163161 set_width : Optional [int ] = None ,
164162 set_height : Optional [int ] = None ,
165163 ):
166-
167164 valid_types : Tuple [Any , ...]
168165
169- if " PIL" in _INSTALLED_MODULES :
166+ if _OptionalDependencies . PIL :
170167 valid_types = (str , Path , PILImage , BytesIO )
171168 else :
172169 valid_types = (str , Path , BytesIO )
@@ -250,7 +247,6 @@ class DataFramePd(Content):
250247 def __init__ (
251248 self , df : "DataFrame" , index : bool = True , col_space : Union [int , str ] = 0
252249 ):
253-
254250 if not isinstance (df , DataFrame ):
255251 raise TypeError (r"df must be Pandas DataFrame" )
256252
@@ -292,7 +288,6 @@ def __init__(
292288 output_format : Optional [str ] = None ,
293289 pdf_figsize : Optional [Union [Tuple [int , int ], float ]] = None ,
294290 ) -> None :
295-
296291 if not isinstance (figure , MplFigure ):
297292 raise TypeError (r"figure must be a Matplotlib Figure" )
298293
@@ -303,7 +298,6 @@ def __init__(
303298 self ._original_figsize = figure .get_size_inches ()
304299
305300 def to_html (self , ** kwargs : bool ) -> str :
306-
307301 if kwargs .get ("notebook_mode" ):
308302 output_format = options .matplotlib .notebook_format
309303 else :
@@ -317,7 +311,6 @@ def to_html(self, **kwargs: bool) -> str:
317311 self .content .set_size_inches (* figsize )
318312
319313 if output_format == "svg" :
320-
321314 string_buffer = StringIO ()
322315 self .content .savefig (string_buffer , format = "svg" )
323316 string_buffer .seek (0 )
@@ -382,7 +375,6 @@ def __init__(
382375 self .layout_attributes = layout_attributes or options .bokeh .layout_attributes
383376
384377 def to_html (self , ** kwargs : bool ) -> str :
385-
386378 if self .layout_attributes :
387379 for key , value in self .layout_attributes .items ():
388380 setattr (self .content , key , value )
@@ -426,7 +418,6 @@ def __init__(
426418 figure : "PlotlyFigure" ,
427419 layout_args : Optional [Dict [Any , Any ]] = None ,
428420 ):
429-
430421 if not isinstance (figure , PlotlyFigure ):
431422 raise TypeError (r"figure must be a Plotly Figure" )
432423
@@ -436,7 +427,6 @@ def __init__(
436427 self ._original_layout = figure .layout
437428
438429 def to_html (self , ** kwargs : bool ) -> str :
439-
440430 if self .layout_args :
441431 self .content .update_layout (** self .layout_args )
442432
@@ -485,7 +475,7 @@ def image_to_bytes(image: Union[str, Path, BytesIO, "PILImage"]) -> BytesIO:
485475 """
486476 if isinstance (image , BytesIO ):
487477 return image
488- elif " PIL" in _INSTALLED_MODULES and isinstance (image , PILImage ):
478+ elif _OptionalDependencies . PIL and isinstance (image , PILImage ):
489479 return BytesIO (image .tobytes ())
490480 elif isinstance (image , (str , Path )):
491481 return BytesIO (Path (image ).read_bytes ())
0 commit comments