Skip to content

Commit

Permalink
Lazy-import pandas (#7851)
Browse files Browse the repository at this point in the history
* Lazy-import pandas

* add changeset

* add changeset

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
  • Loading branch information
whitphx and gradio-pr-bot committed Mar 27, 2024
1 parent 7af3cd7 commit e3b1236
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/sixty-buses-know.md
@@ -0,0 +1,5 @@
---
"gradio": patch
---

feat:Lazy-import pandas
8 changes: 6 additions & 2 deletions gradio/components/bar_plot.py
Expand Up @@ -2,13 +2,15 @@

from __future__ import annotations

from typing import Any, Callable, Literal
from typing import TYPE_CHECKING, Any, Callable, Literal

import pandas as pd
from gradio_client.documentation import document

from gradio.components.plot import AltairPlot, AltairPlotData, Plot

if TYPE_CHECKING:
import pandas as pd


@document()
class BarPlot(Plot):
Expand Down Expand Up @@ -304,4 +306,6 @@ def example_payload(self) -> Any:
return None

def example_value(self) -> Any:
import pandas as pd

return pd.DataFrame({self.x: [1, 2, 3], self.y: [4, 5, 6]})
7 changes: 6 additions & 1 deletion gradio/components/dataframe.py
Expand Up @@ -16,7 +16,6 @@
)

import numpy as np
import pandas as pd
import semantic_version
from gradio_client.documentation import document

Expand All @@ -25,6 +24,7 @@
from gradio.events import Events

if TYPE_CHECKING:
import pandas as pd
import polars as pl # type: ignore
from pandas.io.formats.style import Styler

Expand Down Expand Up @@ -191,6 +191,8 @@ def preprocess(
Returns:
Passes the uploaded spreadsheet data as a `pandas.DataFrame`, `numpy.array`, `polars.DataFrame`, or native 2D Python `list[list]` depending on `type`
"""
import pandas as pd

if self.type == "pandas":
if payload.headers is not None:
return pd.DataFrame(
Expand Down Expand Up @@ -236,6 +238,7 @@ def postprocess(
Returns:
the uploaded spreadsheet data as an object with `headers` and `data` attributes
"""
import pandas as pd
from pandas.io.formats.style import Styler

if value is None:
Expand Down Expand Up @@ -367,6 +370,8 @@ def process_example(
| str
| None,
):
import pandas as pd

if value is None:
return ""
value_df_data = self.postprocess(value)
Expand Down
8 changes: 6 additions & 2 deletions gradio/components/line_plot.py
Expand Up @@ -2,13 +2,15 @@

from __future__ import annotations

from typing import Any, Callable, Literal
from typing import TYPE_CHECKING, Any, Callable, Literal

import pandas as pd
from gradio_client.documentation import document

from gradio.components.plot import AltairPlot, AltairPlotData, Plot

if TYPE_CHECKING:
import pandas as pd


@document()
class LinePlot(Plot):
Expand Down Expand Up @@ -336,4 +338,6 @@ def example_payload(self) -> Any:
return None

def example_value(self) -> Any:
import pandas as pd

return pd.DataFrame({self.x: [1, 2, 3], self.y: [4, 5, 6]})
10 changes: 7 additions & 3 deletions gradio/components/scatter_plot.py
Expand Up @@ -2,14 +2,15 @@

from __future__ import annotations

from typing import Any, Callable, Literal
from typing import TYPE_CHECKING, Any, Callable, Literal

import pandas as pd
from gradio_client.documentation import document
from pandas.api.types import is_numeric_dtype

from gradio.components.plot import AltairPlot, AltairPlotData, Plot

if TYPE_CHECKING:
import pandas as pd


@document()
class ScatterPlot(Plot):
Expand Down Expand Up @@ -231,6 +232,7 @@ def create_plot(
):
"""Helper for creating the scatter plot."""
import altair as alt
from pandas.api.types import is_numeric_dtype

interactive = True if interactive is None else interactive
encodings = {
Expand Down Expand Up @@ -360,4 +362,6 @@ def example_payload(self) -> Any:
return None

def example_value(self) -> Any:
import pandas as pd

return pd.DataFrame({self.x: [1, 2, 3], self.y: [4, 5, 6]})

0 comments on commit e3b1236

Please sign in to comment.