Added Pandas Load HTML, CDA Float Create, CDA Int Create, CDA String Create, CDA Text Create, CDA Show Float, CDA Show Int nodes.

This commit is contained in:
HowToSD
2025-02-19 12:59:11 -08:00
parent 345d9642da
commit 897f3e99f4
27 changed files with 562 additions and 10 deletions
+40
View File
@@ -0,0 +1,40 @@
from typing import Any, Dict
class CDAFloatCreate:
"""
CDA Float Create:
Creates a Python float using a value entered in the text field.
category: IO
"""
@classmethod
def INPUT_TYPES(cls) -> Dict[str, Any]:
"""
Defines the input types for the `create` function.
Returns:
Dict[str, Any]: A dictionary specifying required input types.
"""
return {
"required": {
"data": ("STRING", {"multiline": False}),
}
}
RETURN_TYPES: tuple = ("FLOAT",)
FUNCTION: str = "create"
CATEGORY: str = "Data Analysis"
def create(self, data: str) -> tuple:
"""
creating a Python float for the text entered in the text field.
Args:
data (str): String value in the text field.
Returns:
tuple: A tuple containing the float converted from the text.
"""
return (float(data),)
+41
View File
@@ -0,0 +1,41 @@
from typing import Any, Dict
class CDAIntCreate:
"""
CDA Int Create:
Creates a Python int using a value entered in the text field.
category: IO
"""
@classmethod
def INPUT_TYPES(cls) -> Dict[str, Any]:
"""
Defines the input types for the `create` function.
Returns:
Dict[str, Any]: A dictionary specifying required input types.
"""
return {
"required": {
"data": ("STRING", {"multiline": False}),
}
}
RETURN_TYPES: tuple = ("INT",)
FUNCTION: str = "create"
CATEGORY: str = "Data Analysis"
def create(self, data: str) -> tuple:
"""
creating a Python int for the text entered in the text field.
Args:
data (str): String value in the text field.
Returns:
tuple: A tuple containing the int converted from the text.
"""
return (int(data),)
+62
View File
@@ -0,0 +1,62 @@
"""
This code is based on
https://github.com/pythongosssss/ComfyUI-Custom-Scripts/blob/main/py/show_text.py
See credit/credit.md for the full license.
"""
import sys
class CDAShowFloat:
"""
Displays a floating-point number as text.
category: Display data
"""
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"float_scalar": ("FLOAT", {"default": 0.0, "min": sys.float_info.min, "max": sys.float_info.max})
},
"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, float_scalar, unique_id=None, extra_pnginfo=None):
text = 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:
float_val = float_scalar[0]
text = [str(float_val)] # wrap in list
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 # [text]
else:
print("unique_id or extra_pnginfo is None.")
# "ui" value is the one that is shown on the node UI.
# Note that "ui" return value needs to a list of strings.
return {"ui": {"text": text},
"result": (text,)}
+60
View File
@@ -0,0 +1,60 @@
"""
This code is based on
https://github.com/pythongosssss/ComfyUI-Custom-Scripts/blob/main/py/show_text.py
See credit/credit.md for the full license.
"""
class CDAShowInt:
"""
Displays a Pandas Int as text.
category: Display data
"""
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"int_scalar": ("INT", {"default": 0, "min": -1, "max": 2**31})
},
"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, int_scalar, unique_id=None, extra_pnginfo=None):
text = 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:
int_val = int_scalar[0]
text = [str(int_val)] # wrap in list
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 # [text]
else:
print("unique_id or extra_pnginfo is None.")
# "ui" value is the one that is shown on the node UI.
# Note that "ui" return value needs to a list of strings.
return {"ui": {"text": text},
"result": (text,)}
+41
View File
@@ -0,0 +1,41 @@
from typing import Any, Dict
class CDAStringCreate:
"""
CDA String Create:
Creates a Python String using a value entered in a single-line text field.
category: IO
"""
@classmethod
def INPUT_TYPES(cls) -> Dict[str, Any]:
"""
Defines the input types for the `create` function.
Returns:
Dict[str, Any]: A dictionary specifying required input types.
"""
return {
"required": {
"data": ("STRING", {"multiline": False}),
}
}
RETURN_TYPES: tuple = ("STRING",)
FUNCTION: str = "create"
CATEGORY: str = "Data Analysis"
def create(self, data: str) -> tuple:
"""
creating a Python String for the text entered in the text field.
Args:
data (str): String value in the text field.
Returns:
tuple: A tuple containing the string converted from the text.
"""
return (data,)
+41
View File
@@ -0,0 +1,41 @@
from typing import Any, Dict
class CDATextCreate:
"""
CDA Text Create:
Creates a Python String using a value entered in a multi-line text field.
category: IO
"""
@classmethod
def INPUT_TYPES(cls) -> Dict[str, Any]:
"""
Defines the input types for the `create` function.
Returns:
Dict[str, Any]: A dictionary specifying required input types.
"""
return {
"required": {
"data": ("STRING", {"multiline": True}),
}
}
RETURN_TYPES: tuple = ("STRING",)
FUNCTION: str = "create"
CATEGORY: str = "Data Analysis"
def create(self, data: str) -> tuple:
"""
creating a Python String for the text entered in the text field.
Args:
data (str): String value in the text field.
Returns:
tuple: A tuple containing the string converted from the text.
"""
return (data,)
+2 -2
View File
@@ -31,13 +31,13 @@ class PandasLoadCSV:
def load_csv(self, file_path: str) -> tuple:
"""
Loads a CSV file into a pandas DataFrame and converts it to a JSON string.
Loads a CSV file into a pandas DataFrame.
Args:
file_path (str): The path to the CSV file.
Returns:
tuple: A tuple containing a JSON string representation of the DataFrame.
tuple: A tuple containing a DataFrame.
"""
# Read CSV file into DataFrame
df = pd.read_csv(file_path)
@@ -32,7 +32,7 @@ class PandasLoadCSVWithEncoding:
def load_csv(self, file_path: str, encoding: str) -> tuple:
"""
Loads a CSV file into a pandas DataFrame and converts it to a JSON string. Allows specifying the file encoding to support formats other than UTF-8.
Loads a CSV file into a pandas DataFrame. Allows specifying the file encoding to support formats other than UTF-8.
Refer to the below documents for the list of encodings:
https://docs.python.org/3/library/codecs.html#standard-encodings
@@ -42,7 +42,7 @@ class PandasLoadCSVWithEncoding:
encoding (str): Encoding of the file.
Returns:
tuple: A tuple containing a JSON string representation of the DataFrame.
tuple: A tuple containing a DataFrame.
"""
# Read CSV file into DataFrame
df = pd.read_csv(file_path, encoding=encoding)
@@ -33,14 +33,14 @@ class PandasLoadCSVWithIndex:
def load_csv(self, file_path: str, index_col: int=0) -> tuple:
"""
Loads a CSV file into a pandas DataFrame and converts it to a JSON string.
Loads a CSV file into a pandas DataFrame.
Input data is assume to have an index.
Args:
file_path (str): The path to the CSV file.
Returns:
tuple: A tuple containing a JSON string representation of the DataFrame.
tuple: A tuple containing a DataFrame.
"""
# Read CSV file into DataFrame
df = pd.read_csv(file_path, index_col=index_col)
@@ -0,0 +1,68 @@
from typing import Any, Dict
import pandas as pd
class PandasLoadHTML:
"""
Pandas Load HTML:
Scans HTML files for tables and returns the number of DataFrames, with each table represented as a separate DataFrame and the list of those DataFrames.
category: IO
"""
@classmethod
def INPUT_TYPES(cls) -> Dict[str, Any]:
"""
Defines the input types for the function.
Returns:
Dict[str, Any]: A dictionary specifying required input types.
"""
return {
"required": {
"file_path": ("STRING", {"multiline": False}),
}
}
# TODO: Reference doc generator script cannot parse programatically defined
# list yet, so we need to list return types for now.
RETURN_TYPES: tuple = ("INT",
"DATAFRAME", "DATAFRAME", "DATAFRAME","DATAFRAME",
"DATAFRAME", "DATAFRAME", "DATAFRAME", "DATAFRAME",
"DATAFRAME", "DATAFRAME")
RETURN_NAMES: tuple = ("Number of DataFrames",
"DataFrame1", "DataFrame2", "DataFrame3", "DataFrame4",
"DataFrame5", "DataFrame6", "DataFrame7", "DataFrame8",
"DataFrame9", "DataFrame10")
assert(len(RETURN_NAMES) == len(RETURN_TYPES))
FUNCTION: str = "f"
CATEGORY: str = "Data Analysis"
def f(self, file_path: str) -> tuple:
"""
Loads an HTML file into a pandas DataFrame.
Args:
file_path (str): The path to the HTML file. The path can be a URL.
Returns:
tuple: A tuple containing a DataFrame.
"""
try:
dfs = pd.read_html(file_path, flavor="lxml")
except:
dfs = []
num_dfs = 0
if dfs is None:
num_dfs = 0
dfs = []
else:
num_dfs = len(dfs)
df_slot_count = len(PandasLoadHTML.RETURN_TYPES) - 1
filler_count = df_slot_count - num_dfs
empty_dfs = [pd.DataFrame({})] * filler_count # If filler = 0, this will still return []
dfs_out = dfs + empty_dfs
return tuple([num_dfs] + dfs_out)
+2 -2
View File
@@ -31,13 +31,13 @@ class PandasLoadJSON:
def load_json(self, file_path: str) -> tuple:
"""
Loads a JSON file into a pandas DataFrame and converts it to a JSON string.
Loads a JSON file into a pandas DataFrame.
Args:
file_path (str): The path to the JSON file.
Returns:
tuple: A tuple containing a JSON string representation of the DataFrame.
tuple: A tuple containing a DataFrame.
"""
# Read JSON file into DataFrame
df = pd.read_json(file_path)