Skip to content

Commit

Permalink
refactor optional feature handling to preserve signature of ui.plotly…
Browse files Browse the repository at this point in the history
… etc.
  • Loading branch information
falkoschindler committed Jul 26, 2023
1 parent a74a9b5 commit 39f9c30
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 31 deletions.
14 changes: 12 additions & 2 deletions nicegui/elements/plotly.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
from typing import Dict, Union
from __future__ import annotations

import plotly.graph_objects as go
from typing import Dict, Union

from .. import globals
from ..element import Element

try:
import plotly.graph_objects as go
globals.optional_features.add('plotly')
except ImportError:
pass


class Plotly(Element, component='plotly.vue', libraries=['lib/plotly/plotly.min.js']):

Expand All @@ -22,6 +29,9 @@ def __init__(self, figure: Union[Dict, go.Figure]) -> None:
:param figure: Plotly figure to be rendered. Can be either a `go.Figure` instance, or
a `dict` object with keys `data`, `layout`, `config` (optional).
"""
if not 'plotly' in globals.optional_features:
raise ImportError('Plotly is not installed. Please run "pip install nicegui[plotly]".')

super().__init__()

self.figure = figure
Expand Down
13 changes: 11 additions & 2 deletions nicegui/elements/pyplot.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import asyncio
import io
import os
from typing import Any

import matplotlib.pyplot as plt

from .. import background_tasks, globals
from ..element import Element

try:
if os.environ.get('MATPLOTLIB', 'true').lower() == 'true':
import matplotlib.pyplot as plt
globals.optional_features.add('matplotlib')
except ImportError:
pass


class Pyplot(Element):

Expand All @@ -18,6 +24,9 @@ def __init__(self, *, close: bool = True, **kwargs: Any) -> None:
:param close: whether the figure should be closed after exiting the context; set to `False` if you want to update it later (default: `True`)
:param kwargs: arguments like `figsize` which should be passed to `pyplot.figure <https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.figure.html>`_
"""
if 'matplotlib' not in globals.optional_features:
raise ImportError('Matplotlib is not installed. Please run "pip install matplotlib".')

super().__init__('div')
self.close = close
self.fig = plt.figure(**kwargs)
Expand Down
33 changes: 6 additions & 27 deletions nicegui/ui.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import os

__all__ = [
'deprecated',
'element',
Expand Down Expand Up @@ -34,6 +32,7 @@
'keyboard',
'knob',
'label',
'line_plot',
'link',
'link_target',
'log',
Expand All @@ -42,8 +41,10 @@
'menu_item',
'mermaid',
'number',
'plotly',
'circular_progress',
'linear_progress',
'pyplot',
'query',
'radio',
'row',
Expand Down Expand Up @@ -124,6 +125,7 @@
from .elements.keyboard import Keyboard as keyboard
from .elements.knob import Knob as knob
from .elements.label import Label as label
from .elements.line_plot import LinePlot as line_plot
from .elements.link import Link as link
from .elements.link import LinkTarget as link_target
from .elements.log import Log as log
Expand All @@ -132,8 +134,10 @@
from .elements.menu import MenuItem as menu_item
from .elements.mermaid import Mermaid as mermaid
from .elements.number import Number as number
from .elements.plotly import Plotly as plotly
from .elements.progress import CircularProgress as circular_progress
from .elements.progress import LinearProgress as linear_progress
from .elements.pyplot import Pyplot as pyplot
from .elements.query import query
from .elements.radio import Radio as radio
from .elements.row import Row as row
Expand Down Expand Up @@ -177,28 +181,3 @@
from .page_layout import RightDrawer as right_drawer
from .run import run
from .run_with import run_with

try:
from .elements.plotly import Plotly as plotly
globals.optional_features.add('plotly')
except ImportError:
def plotly(*args, **kwargs):
raise ImportError('Plotly is not installed. Please run "pip install plotly".')
__all__.append('plotly')

if os.environ.get('MATPLOTLIB', 'true').lower() == 'true':
try:
from .elements.line_plot import LinePlot as line_plot
from .elements.pyplot import Pyplot as pyplot
plot = deprecated(pyplot, 'ui.plot', 'ui.pyplot', 317)
globals.optional_features.add('matplotlib')
except ImportError:
def line_plot(*args, **kwargs):
raise ImportError('Matplotlib is not installed. Please run "pip install matplotlib".')

def pyplot(*args, **kwargs):
raise ImportError('Matplotlib is not installed. Please run "pip install matplotlib".')

def plot(*args, **kwargs):
raise ImportError('Matplotlib is not installed. Please run "pip install matplotlib".')
__all__.extend(['line_plot', 'pyplot', 'plot'])

0 comments on commit 39f9c30

Please sign in to comment.