class Example: “”“ A example node Class methods ------------- INPUTTYPES (dict): Tell…
- User
class Example: “”“ A example node
Class methods
INPUT_TYPES (dict): Tell the main program input parameters of nodes.
Attributes
RETURN_TYPES (tuple): The type of each element in the output tulple. RETURN_NAMES (tuple): Optional: The name of each output in the output tulple. FUNCTION (str): The name of the entry-point method. For example, if FUNCTION = "execute" then it will run Example().execute() OUTPUT_NODE ([bool]): If this node is an output node that outputs a result/image from the graph. The SaveImage node is an example. The backend iterates on these output nodes and tries to execute all their parents if their parent graph is properly connected. Assumed to be False if not present. CATEGORY (str): The category the node should appear in the UI. execute(s) -> tuple || None: The entry point method. The name of this method must be the same as the value of property FUNCTION. For example, if FUNCTION = "execute" then this method’s name must be execute, if FUNCTION = "foo" then it must be foo. “”” def init(self): pass
@classmethod def INPUT_TYPES(s): “”“ Return a dictionary which contains config for all input fields. Some types (string): “MODEL”, “VAE”, “CLIP”, “CONDITIONING”, “LATENT”, “IMAGE”, “INT”, “STRING”, “FLOAT”. Input types “INT”, “STRING” or “FLOAT” are special values for fields on the node. The type can be a list for selection.
Returns: dict:
- Key input_fields_group (string): Can be either required, hidden or optional. A node class must have property required
- Value input_fields (dict): Contains input fields config:
- Key field_name (string): Name of a entry-point method’s argument
- Value field_config (tuple):
- First value is a string indicate the type of field or a list for selection.
- Secound value is a config for type “INT”, “STRING” or “FLOAT”. “”” return { “required”: { “image”: (“IMAGE”,), “int_field”: (“INT”, { “default”: 0, “min”: 0, #Minimum value “max”: 4096, #Maximum value “step”: 64, #Slider’s step “display”: “number” # Cosmetic only: display as “number” or “slider” }), “float_field”: (“FLOAT”, { “default”: 1.0, “min”: 0.0, “max”: 10.0, “step”: 0.01, “round”: 0.001, #The value represeting the precision to round to, will be set to the step value by default. Can be set to False to disable rounding. “display”: “number”}), “print_to_screen”: ([“enable”, “disable”],), “string_field”: (“STRING”, { “multiline”: False, #True if you want the field to look like the one on the ClipTextEncode node “default”: “Hello World!” }), }, }
RETURN_TYPES = (“IMAGE”,) #RETURN_NAMES = (“image_output_name”,)
FUNCTION = “test”
#OUTPUT_NODE = False
CATEGORY = “Example”
def test(self, image, string_field, int_field, float_field, print_to_screen): if print_to_screen == “enable”: print(f""“Your input contains: string_field aka input text: {string_field} int_field: {int_field} float_field: {float_field} “””) #do some processing on the image, in this example I just invert it image = 1.0 - image return (image,)
- Assistant
ComfyUI是一个用户不可见的内部项目。假定你需要自定义节点来加载和查看Excel文件,我们可以参考你给出的示例代码来构建两个新的类:
ExcelLoader和ExcelViewer。由于实际加载和解析Excel文件需要做一些外部的操作,所以我会使用一个假想的库openpyxl来表示这一功能。在下面的示例代码中,我会包括一些必要的占位符方法,这些方法将需要你自己提供实现,以根据你的具体需求进行完整的Excel处理。python - User
还需要一个 init.py 文件。又可以放到这个文件里面的内容吗?
- Assistant
在一个Python模块中,
__init__.py文件的作用是将一个目录标识为Python的包。这个文件可以为空,但也可以包含初始化代码或将部分模块导出为包的API。假设你有一个名为custom_nodes的目录,里面包括刚刚提到的ExcelLoader和ExcelViewer类的代码。那么,你的__init__.py文件可能会包含以下内容:python# custom_nodes/__init__.py # 导入自定义节点的类 from .excel_loader import ExcelLoader from .excel_viewer import ExcelViewer # 节点类映射,用于根据节点名字识别对应的Python类 NODE_CLASS_MAPPINGS = { "ExcelLoader": ExcelLoader, "ExcelViewer": ExcelViewer } # 节点显示名称映射,用于UI中节点的友好名称显示 NODE_DISPLAY_NAME_MAPPINGS = { "ExcelLoader": "Load Excel File", "ExcelViewer": "View Excel Data" } # 这里可以初始化一些包级别的配置或者状态 def initialize_custom_nodes(): # 自定义初始化逻辑 pass # 初始化操作 initialize_custom_nodes()假设
ExcelLoader和定义在和中,并与位于同一目录中,上面的和语句会正确地从这些文件中导入类定义。 - User
import openpyxl
class ExcelLoader: @classmethod def INPUT_TYPES(cls): return { "required": { "file_path": ("STRING", {"default": ""}), } }
codeRETURN_TYPES = ("EXCEL_DATA",) FUNCTION = "load_excel" CATEGORY = "Offeic模块组/Excel" RETURN_NAMES = ("data",) def load_excel(self, file_path): workbook = openpyxl.load_workbook(file_path) data = self.parse_excel_file(workbook) return (data,) @staticmethod def parse_excel_file(workbook): # To be implemented: Convert workbook into a custom Excel data representation passclass ExcelViewer: @classmethod def INPUT_TYPES(cls): return { "required": { "excel_data": ("EXCEL_DATA"), } }
code - Assistant
要将Excel文件加载成dataframe格式,我们可以使用
pandas库,它提供了非常方便的方法来读取和显示Excel文件。在ExcelLoader中,我们将使用pandas.read_excel来加载Excel文件。然后ExcelViewer将显示dataframe中的数据。首先,请确保你已经安装了
pandas。你可以使用pip来安装它:bashpip install pandas现在,让我们更新前面的代码,使其可以加载Excel文件为dataframe,并在
ExcelViewer中显示这些数据。python