Skip to content

feat: Update dataframe_operations.py #7713

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import matplotlib.pyplot as plt
import pandas as pd

from langflow.custom import Component
from langflow.io import BoolInput, DataFrameInput, DropdownInput, IntInput, MessageTextInput, Output, StrInput
from langflow.schema import DataFrame
Expand All @@ -19,6 +22,7 @@
"Select Columns",
"Sort",
"Tail",
"Base64",
]

inputs = [
Expand Down Expand Up @@ -107,7 +111,7 @@
name="output",
method="perform_operation",
info="The resulting DataFrame after the operation.",
)
),
]

def update_build_config(self, build_config, field_value, field_name=None):
Expand Down Expand Up @@ -175,8 +179,10 @@
return self.tail(dataframe_copy)
if operation == "Replace Value":
return self.replace_values(dataframe_copy)
msg = f"Unsupported operation: {operation}"
if operation == "Base64":
return self.convert_dataframe_to_base64(dataframe_copy)

msg = f"Unsupported operation: {operation}"
raise ValueError(msg)

# Existing methods
Expand Down Expand Up @@ -210,3 +216,20 @@
def replace_values(self, df: DataFrame) -> DataFrame:
df[self.column_name] = df[self.column_name].replace(self.replace_value, self.replacement_value)
return DataFrame(df)

def convert_dataframe_to_base64(self, df: DataFrame) -> DataFrame:
try:
df_base64 = pd.DataFrame(df).copy()
fig, ax = plt.subplots(figsize=(len(df_base64.columns) * 1.2, len(df_base64) * 0.5))
ax.axis("tight")
ax.axis("off")
ax.table(cellText=df.values, colLabels=df_base64.columns, loc="center")
buf = io.BytesIO()

Check failure on line 227 in src/backend/base/langflow/components/processing/dataframe_operations.py

View workflow job for this annotation

GitHub Actions / Ruff Style Check (3.13)

Ruff (F821)

src/backend/base/langflow/components/processing/dataframe_operations.py:227:19: F821 Undefined name `io`
plt.savefig(buf, format="png", bbox_inches="tight", dpi=150)
plt.close(fig)
buf.seek(0)
base64_image = base64.b64encode(buf.read()).decode("utf-8")

Check failure on line 231 in src/backend/base/langflow/components/processing/dataframe_operations.py

View workflow job for this annotation

GitHub Actions / Ruff Style Check (3.13)

Ruff (F821)

src/backend/base/langflow/components/processing/dataframe_operations.py:231:28: F821 Undefined name `base64`
return DataFrame(pd.DataFrame({"base64_image": [base64_image]}))
except Exception as e:

Check failure on line 233 in src/backend/base/langflow/components/processing/dataframe_operations.py

View workflow job for this annotation

GitHub Actions / Ruff Style Check (3.13)

Ruff (BLE001)

src/backend/base/langflow/components/processing/dataframe_operations.py:233:16: BLE001 Do not catch blind exception: `Exception`
self.status = f"Error: {e!s}"
return DataFrame(pd.DataFrame({"error": [str(e)]}))
Loading