Added Show Text, Columns, Index nodes
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
from typing import Any, Dict
|
||||
from io import StringIO
|
||||
import pandas as pd
|
||||
from .utils import index_to_jsons
|
||||
|
||||
class PandasColumns:
|
||||
"""
|
||||
PandasColumns:
|
||||
A class for retrieving the column labels of a pandas DataFrame.
|
||||
"""
|
||||
|
||||
@classmethod
|
||||
def INPUT_TYPES(cls) -> Dict[str, Any]:
|
||||
"""
|
||||
Defines the input types for the `columns` function.
|
||||
|
||||
Returns:
|
||||
Dict[str, Any]: A dictionary specifying required input types.
|
||||
"""
|
||||
return {
|
||||
"required": {
|
||||
"dataframe_json": ("STRING", {"multiline": True})
|
||||
}
|
||||
}
|
||||
|
||||
RETURN_TYPES: tuple = ("STRING",)
|
||||
FUNCTION: str = "columns"
|
||||
CATEGORY: str = "Data Analysis"
|
||||
|
||||
def columns(self, dataframe_json: str) -> tuple:
|
||||
"""
|
||||
Returns the column labels of a pandas DataFrame.
|
||||
|
||||
Args:
|
||||
dataframe_json (str): A JSON string representation of the DataFrame.
|
||||
|
||||
Returns:
|
||||
tuple: A tuple containing a JSON string for the serialized pandas.Index containing columns.
|
||||
"""
|
||||
# Deserialize JSON string to DataFrame
|
||||
df = pd.read_json(StringIO(dataframe_json))
|
||||
columns = df.columns
|
||||
return (index_to_jsons(columns),)
|
||||
@@ -0,0 +1,43 @@
|
||||
from typing import Any, Dict
|
||||
from io import StringIO
|
||||
import pandas as pd
|
||||
from .utils import index_to_jsons
|
||||
|
||||
class PandasIndex:
|
||||
"""
|
||||
PandasColumns:
|
||||
A class for retrieving the row labels (index) of a pandas DataFrame.
|
||||
"""
|
||||
|
||||
@classmethod
|
||||
def INPUT_TYPES(cls) -> Dict[str, Any]:
|
||||
"""
|
||||
Defines the input types for the `row_index` function.
|
||||
|
||||
Returns:
|
||||
Dict[str, Any]: A dictionary specifying required input types.
|
||||
"""
|
||||
return {
|
||||
"required": {
|
||||
"dataframe_json": ("STRING", {"multiline": True})
|
||||
}
|
||||
}
|
||||
|
||||
RETURN_TYPES: tuple = ("STRING",)
|
||||
FUNCTION: str = "row_index"
|
||||
CATEGORY: str = "Data Analysis"
|
||||
|
||||
def row_index(self, dataframe_json: str) -> tuple:
|
||||
"""
|
||||
Returns the row labels (index) of a pandas DataFrame.
|
||||
|
||||
Args:
|
||||
dataframe_json (str): A JSON string representation of the DataFrame.
|
||||
|
||||
Returns:
|
||||
tuple: A tuple containing a JSON string for the serialized pandas.Index containing row labels.
|
||||
"""
|
||||
# Deserialize JSON string to DataFrame
|
||||
df = pd.read_json(StringIO(dataframe_json))
|
||||
row_index = df.index
|
||||
return (index_to_jsons(row_index),)
|
||||
@@ -12,7 +12,7 @@ class PandasJoin:
|
||||
@classmethod
|
||||
def INPUT_TYPES(cls) -> Dict[str, Any]:
|
||||
"""
|
||||
Defines the input types for the `sort_rows` function.
|
||||
Defines the input types for the `join` function.
|
||||
|
||||
Returns:
|
||||
Dict[str, Any]: A dictionary specifying required input types.
|
||||
@@ -0,0 +1,48 @@
|
||||
"""
|
||||
Almost all of this code was taken from
|
||||
https://github.com/pythongosssss/ComfyUI-Custom-Scripts/blob/main/py/show_text.py
|
||||
|
||||
See credit/credit.md for the full license.
|
||||
"""
|
||||
|
||||
class PandasShowText:
|
||||
@classmethod
|
||||
def INPUT_TYPES(s):
|
||||
return {
|
||||
"required": {
|
||||
"text": ("STRING", {"forceInput": True}),
|
||||
},
|
||||
"hidden": {
|
||||
"unique_id": "UNIQUE_ID",
|
||||
"extra_pnginfo": "EXTRA_PNGINFO",
|
||||
},
|
||||
}
|
||||
|
||||
INPUT_IS_LIST = True
|
||||
RETURN_TYPES = ("STRING",)
|
||||
FUNCTION = "notify"
|
||||
OUTPUT_NODE = True
|
||||
OUTPUT_IS_LIST = (True,)
|
||||
|
||||
CATEGORY = "Data Analysis"
|
||||
|
||||
def notify(self, text, unique_id=None, extra_pnginfo=None):
|
||||
if unique_id is not None and extra_pnginfo is not None:
|
||||
if not isinstance(extra_pnginfo, list):
|
||||
print("Error: extra_pnginfo is not a list")
|
||||
elif (
|
||||
not isinstance(extra_pnginfo[0], dict)
|
||||
or "workflow" not in extra_pnginfo[0]
|
||||
):
|
||||
print("Error: extra_pnginfo[0] is not a dict or missing 'workflow' key")
|
||||
else:
|
||||
workflow = extra_pnginfo[0]["workflow"]
|
||||
node = next(
|
||||
(x for x in workflow["nodes"] if str(x["id"]) == str(unique_id[0])),
|
||||
None,
|
||||
)
|
||||
if node:
|
||||
node["widgets_values"] = [text]
|
||||
else:
|
||||
print("unique_id or extra_pnginfo is None.")
|
||||
return {"ui": {"text": text}, "result": (text,)}
|
||||
@@ -0,0 +1,63 @@
|
||||
import json
|
||||
import pandas as pd
|
||||
|
||||
def series_to_jsons(series: pd.Series) -> str:
|
||||
"""
|
||||
Convert a pandas Series to a JSON string.
|
||||
|
||||
Args:
|
||||
series (pd.Series): The pandas Series to convert.
|
||||
|
||||
Returns:
|
||||
str: A JSON string representing the Series, including its data, index, and name.
|
||||
"""
|
||||
array = [x.item() if hasattr(x, "item") else x for x in series.tolist()] # Convert NumPy types to Python types
|
||||
index = [x.item() if hasattr(x, "item") else x for x in series.index.tolist()] # Convert index to Python types
|
||||
name = series.name
|
||||
|
||||
d = {"array": array, "index": index, "name": name}
|
||||
|
||||
return json.dumps(d)
|
||||
|
||||
def jsons_to_series(s: str) -> pd.Series:
|
||||
"""
|
||||
Convert a JSON string to a pandas Series.
|
||||
|
||||
Args:
|
||||
s (str): The JSON string representing the Series.
|
||||
|
||||
Returns:
|
||||
pd.Series: The reconstructed pandas Series.
|
||||
"""
|
||||
d = json.loads(s)
|
||||
return pd.Series(data=d["array"], index=d["index"], name=d["name"])
|
||||
|
||||
def index_to_jsons(index: pd.Index) -> str:
|
||||
"""
|
||||
Convert a pandas Index to a JSON string.
|
||||
|
||||
Args:
|
||||
index (pd.Index): The pandas Index to convert.
|
||||
|
||||
Returns:
|
||||
str: A JSON string representing the Index, including its values and name.
|
||||
"""
|
||||
index_list = [x.item() if hasattr(x, "item") else x for x in index.tolist()] # Convert NumPy types to Python types
|
||||
index_name = index.name # Preserve index name
|
||||
|
||||
d = {"index": index_list, "name": index_name}
|
||||
|
||||
return json.dumps(d)
|
||||
|
||||
def jsons_to_index(s: str) -> pd.Index:
|
||||
"""
|
||||
Convert a JSON string to a pandas Index.
|
||||
|
||||
Args:
|
||||
s (str): The JSON string representing the Index.
|
||||
|
||||
Returns:
|
||||
pd.Index: The reconstructed pandas Index.
|
||||
"""
|
||||
d = json.loads(s)
|
||||
return pd.Index(d["index"], name=d.get("name")) # Restore index with name
|
||||
Reference in New Issue
Block a user