Added Pandas Load JSON and Pandas Save JSON nodes.
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
from typing import Any, Dict
|
||||
from io import StringIO
|
||||
import pandas as pd
|
||||
|
||||
|
||||
class PandasLoadJSON:
|
||||
"""
|
||||
PandasLoadJSON:
|
||||
A class for loading JSON files into a pandas DataFrame.
|
||||
"""
|
||||
|
||||
@classmethod
|
||||
def INPUT_TYPES(cls) -> Dict[str, Any]:
|
||||
"""
|
||||
Defines the input types for the `load_json` function.
|
||||
|
||||
Returns:
|
||||
Dict[str, Any]: A dictionary specifying required input types.
|
||||
"""
|
||||
return {
|
||||
"required": {
|
||||
"file_path": ("STRING", {"multiline": False}),
|
||||
}
|
||||
}
|
||||
|
||||
RETURN_TYPES: tuple = ("STRING",)
|
||||
FUNCTION: str = "load_json"
|
||||
CATEGORY: str = "Data Analysis"
|
||||
|
||||
def load_json(self, file_path: str) -> tuple:
|
||||
"""
|
||||
Loads a JSON file into a pandas DataFrame and converts it to a JSON string.
|
||||
|
||||
Args:
|
||||
file_path (str): The path to the JSON file.
|
||||
|
||||
Returns:
|
||||
tuple: A tuple containing a JSON string representation of the DataFrame.
|
||||
"""
|
||||
# Read JSON file into DataFrame
|
||||
df = pd.read_json(file_path)
|
||||
|
||||
# Convert DataFrame to JSON
|
||||
result_json = df.to_json()
|
||||
|
||||
return (result_json,)
|
||||
@@ -0,0 +1,63 @@
|
||||
"""
|
||||
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.
|
||||
"""
|
||||
from typing import Any, Dict
|
||||
from io import StringIO
|
||||
import pandas as pd
|
||||
|
||||
class PandasSaveJSON:
|
||||
"""
|
||||
PandasSaveJSON:
|
||||
A class for saving a PandasDataFrame to a JSON file.
|
||||
"""
|
||||
@classmethod
|
||||
def INPUT_TYPES(s):
|
||||
return {
|
||||
"required": {
|
||||
"dataframe_json": ("STRING", {"forceInput": True}),
|
||||
"file_path": ("STRING", {"multiline": False})
|
||||
},
|
||||
"hidden": {
|
||||
"unique_id": "UNIQUE_ID",
|
||||
"extra_pnginfo": "EXTRA_PNGINFO",
|
||||
},
|
||||
}
|
||||
|
||||
INPUT_IS_LIST = True
|
||||
RETURN_TYPES = ("STRING",)
|
||||
FUNCTION = "save_json"
|
||||
OUTPUT_NODE = True
|
||||
OUTPUT_IS_LIST = (True,)
|
||||
|
||||
CATEGORY = "Data Analysis"
|
||||
|
||||
def save_json(self, dataframe_json,file_path=None, 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")
|
||||
elif file_path is None or len(file_path) == 0:
|
||||
print("Error: file path is not specified")
|
||||
else:
|
||||
# Deserialize JSON string to DataFrame
|
||||
df = pd.read_json(StringIO(dataframe_json[0]))
|
||||
df.to_json(path_or_buf=file_path[0]) # Save as JSON
|
||||
text = [str(df)] # 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]
|
||||
else:
|
||||
print("unique_id or extra_pnginfo is None.")
|
||||
return {"ui": {"text": text}, "result": (text,)}
|
||||
Reference in New Issue
Block a user