Skip to content

Code

The Code node enables execution of custom Python code within browser workflows using Pyodide, providing unlimited flexibility for data processing, scientific computing, and complex automation logic. This node bridges the gap between built-in node functionality and custom requirements, allowing developers to implement sophisticated workflows with the full power of Python and its ecosystem.

sequenceDiagram
    participant Input as Workflow Data
    participant Code as Code Node
    participant Pyodide as Pyodide Runtime
    participant Packages as Package Manager
    participant Python as Python Interpreter
    participant Output as Results Output

    Input->>Code: Input data + Python code
    Code->>Pyodide: Initialize runtime
    Pyodide->>Packages: Install required packages
    Packages->>Pyodide: Package dependencies ready
    Code->>Python: Execute Python code
    Python->>Python: Process data with libraries
    Python->>Code: Return execution results
    Code->>Code: Capture stdout/stderr
    Code->>Code: Add execution metadata
    Code->>Output: Structured results + logs

    Note over Pyodide: WebAssembly sandbox
    Note over Python: Full Python ecosystem

This node performs custom code execution by:

  • Running Python code in the browser using Pyodide WebAssembly runtime
  • Processing data from previous workflow nodes with Python’s rich ecosystem
  • Implementing complex data analysis, machine learning, and scientific computing
  • Accessing popular Python libraries like NumPy, Pandas, SciPy, and more
  • Performing advanced mathematical operations and statistical analysis
  • Full Python Support: Execute any valid Python code with access to the standard library
  • Scientific Computing: Built-in access to NumPy, Pandas, Matplotlib, and other scientific libraries
  • Data Integration: Seamless access to data from previous workflow nodes
  • WebAssembly Performance: Near-native Python execution speed in the browser
  • Package Management: Install and use additional Python packages via micropip
  • Data Science & Analytics: Perform statistical analysis, data visualization, and machine learning
  • Scientific Computing: Complex mathematical operations, numerical analysis, and simulations
  • Advanced Data Processing: Transform and analyze large datasets with Pandas and NumPy
  • Custom Algorithms: Implement specialized algorithms for data processing and analysis
  • Text Processing: Natural language processing, text analysis, and content extraction
ParameterTypeDescriptionExample
codestringThe Python code to execute"import pandas as pd\nreturn df.describe()"
ParameterTypeDefaultDescriptionExample
timeoutnumber30000Maximum execution time in milliseconds10000
returnTypestring"auto"Expected return type (auto, json, string, number)"json"
packagesarray[]Additional Python packages to install via micropip["requests", "beautifulsoup4"]
memoryLimitstring"100MB"Maximum memory usage for Python execution"256MB"
{
"code": "import pandas as pd\nimport numpy as np\ndata = input_data['PreviousNode']\ndf = pd.DataFrame(data)\nreturn df.describe().to_dict()",
"timeout": 15000,
"returnType": "json",
"packages": ["pandas", "numpy", "matplotlib"],
"memoryLimit": "256MB",
"executionOptions": {
"enableStdout": true,
"enableStderr": true,
"maxOutputSize": "10MB"
}
}
ComponentPurposePerformance Impact
PyodideWebAssembly-based Python runtime in browserNear-native Python performance with initial load time
micropipPackage installer for additional Python librariesNetwork requests for package downloads
  • Core Libraries: Built-in Python standard library (json, re, datetime, etc.)
  • Scientific Stack: NumPy, Pandas, SciPy, Matplotlib, Scikit-learn
  • Data Processing: Requests, BeautifulSoup4, Pillow, OpenCV
  • Machine Learning: TensorFlow.js integration, Scikit-learn, XGBoost
FeatureChromeFirefoxSafariEdge
Pyodide Runtime✅ Full✅ Full✅ Full✅ Full
Scientific Libraries✅ Full✅ Full✅ Full✅ Full
Package Installation✅ Full✅ Full⚠️ Limited✅ Full
WebAssembly Support✅ Full✅ Full✅ Full✅ Full
  • Sandboxed Execution: Python code runs in isolated Pyodide environment with no direct system access
  • Memory Isolation: WebAssembly provides memory safety and prevents buffer overflows
  • Network Restrictions: Limited network access through controlled package installation
  • Execution Timeout: Configurable timeouts prevent infinite loops and resource exhaustion
  • Package Validation: Only verified packages from PyPI can be installed via micropip
{
"code": "string",
"options": {
"timeout": "number",
"returnType": "string",
"packages": "array",
"memoryLimit": "string"
},
"input_data": {
"previousNodeData": "object",
"workflowVariables": "object"
}
}
{
"result": "any",
"executionTime": "number",
"stdout": "string",
"stderr": "string",
"metadata": {
"codeLength": "number",
"returnType": "string",
"timestamp": "ISO_8601_string",
"memoryUsage": "number",
"packagesInstalled": "array",
"pyodideVersion": "string",
"errors": "array"
}
}

Practical Examples

Scenario: Analyze extracted data to generate statistical insights and visualizations

Configuration:

{
"code": "import pandas as pd\nimport numpy as np\n\ntext = input_data['GetAllText']['text']\nwords = text.split()\nword_count = len(words)\nreading_time = np.ceil(word_count / 200)\nsentences = len([s for s in text.split('.') if s.strip()])\n\nresult = {\n 'word_count': word_count,\n 'reading_time': int(reading_time),\n 'sentences': sentences,\n 'avg_words_per_sentence': round(word_count / sentences) if sentences > 0 else 0\n}\n\nresult",
"timeout": 5000,
"returnType": "json",
"packages": ["pandas", "numpy"]
}

Input Data:

{
"GetAllText": {
"text": "This is a sample article with multiple sentences. It contains multiple pieces of information that needs to be analyzed. The content is rich and informative for readers."
}
}

Expected Output:

{
"result": {
"word_count": 28,
"reading_time": 1,
"sentences": 3,
"avg_words_per_sentence": 9
},
"executionTime": 156,
"stdout": "",
"stderr": "",
"metadata": {
"codeLength": 387,
"returnType": "json",
"timestamp": "2024-01-15T10:30:00Z",
"memoryUsage": 2048,
"packagesInstalled": ["pandas", "numpy"],
"pyodideVersion": "0.24.1",
"errors": []
}
}

Step-by-Step Process

flowchart TD
    A[Input Data from Previous Node] --> B[Code Node]
    B --> C[Initialize Pyodide Runtime]
    C --> D{Packages Required?}
    D -->|Yes| E[Install Python Packages]
    D -->|No| F[Execute Python Code]
    E --> F
    F --> G[Import Libraries]
    G --> H[Process Input Data]
    H --> I[Perform Calculations]
    I --> J[Generate Results]
    J --> K{Execution Successful?}
    K -->|Yes| L[Format Output Data]
    K -->|No| M[Capture Error Information]
    L --> N[Add Execution Metadata]
    M --> N
    N --> O[Return Structured Results]

    style B fill:#e1f5fe
    style E fill:#fff3e0
    style I fill:#f3e5f5
    style O fill:#e8f5e8
  1. Import required Python libraries (Pandas, NumPy)
  2. Extract text data from previous workflow node
  3. Process text using Python string methods and NumPy functions
  4. Calculate reading statistics with proper error handling
  5. Return structured analysis data as Python dictionary

Example 2: Machine Learning Data Processing

Section titled “Example 2: Machine Learning Data Processing”

Scenario: Process and analyze dataset using scikit-learn for pattern recognition

Configuration:

{
"code": "import pandas as pd\nimport numpy as np\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.cluster import KMeans\n\n# Get data from previous node\ndata = input_data['DataExtraction']['records']\ndf = pd.DataFrame(data)\n\n# Prepare numeric columns for analysis\nnumeric_cols = df.select_dtypes(include=[np.number]).columns\nX = df[numeric_cols].fillna(0)\n\n# Standardize the data\nscaler = StandardScaler()\nX_scaled = scaler.fit_transform(X)\n\n# Perform clustering\nkmeans = KMeans(n_clusters=3, random_state=42)\nclusters = kmeans.fit_predict(X_scaled)\n\n# Add cluster labels to original data\ndf['cluster'] = clusters\n\n# Generate summary statistics\nsummary = {\n 'total_records': len(df),\n 'clusters_found': len(np.unique(clusters)),\n 'cluster_distribution': pd.Series(clusters).value_counts().to_dict(),\n 'feature_importance': dict(zip(numeric_cols, np.abs(kmeans.cluster_centers_).mean(axis=0)))\n}\n\nsummary",
"timeout": 15000,
"packages": ["pandas", "numpy", "scikit-learn"],
"memoryLimit": "256MB"
}

Workflow Integration:

Data Extraction → Code (ML Analysis) → Results Visualization → Report Generation
↓ ↓ ↓ ↓
raw_data processed_clusters visual_insights final_report

Complete Example: This code performs unsupervised machine learning analysis on extracted data, identifying patterns and clusters that can inform business decisions or further analysis.

This example demonstrates the fundamental usage of the Code node in a typical workflow scenario.

Configuration:

{
"url": "example_value",
"followRedirects": true
}

Input Data:

{
"data": "sample input data"
}

Expected Output:

{
"result": "processed output data"
}

This example shows more complex configuration options and integration patterns.

Configuration:

{
"parameter1": "advanced_value",
"parameter2": false,
"advancedOptions": {
"option1": "value1",
"option2": 100
}
}

Example showing how this node integrates with other workflow nodes:

  1. Previous NodeCodeNext Node
  2. Data flows through the workflow with appropriate transformations
  3. Error handling and validation at each step
  • Nodes: Data Extraction → Code (Python Analysis) → Visualization → Results Export
  • Use Case: Complex statistical analysis, machine learning, and scientific computing
  • Configuration Tips: Install required packages, set appropriate memory limits, handle large datasets efficiently
  • Nodes: Content Extraction → Code (NLP Processing) → Sentiment Analysis → Report Generation
  • Use Case: Natural language processing, text analysis, and content intelligence
  • Data Flow: Extract text content, apply Python NLP libraries, generate insights and reports
  • Package Management: Only install necessary packages to minimize load time and memory usage
  • Error Handling: Use try-except blocks to handle runtime errors and provide meaningful error messages
  • Memory Efficiency: Use generators and iterators for large datasets, avoid loading entire datasets into memory
  • Code Organization: Structure code with functions and classes for better maintainability and reusability
  • Data Validation: Validate input data structure and types before processing
  • Symptoms: Code node fails when trying to import or use additional packages
  • Causes: Network connectivity issues, package compatibility, or incorrect package names
  • Solutions:
    1. Verify package names are correct and available on PyPI
    2. Check network connectivity and firewall restrictions
    3. Use alternative packages or built-in libraries when possible
    4. Install packages one at a time to identify problematic dependencies
  • Prevention: Test package installation in a separate environment first
  • Symptoms: Code execution fails with out-of-memory errors
  • Causes: Large dataset processing, memory leaks, or inefficient algorithms
  • Solutions:
    1. Increase memory limit in node configuration
    2. Process data in smaller chunks using generators or iterators
    3. Use more memory-efficient data structures and algorithms
    4. Clear variables and use garbage collection when processing large datasets
  • Prevention: Profile memory usage during development and optimize accordingly
  • Not all Python packages are available in Pyodide’s WebAssembly environment
  • Some packages may have limited functionality compared to native Python installations
  • Initial Pyodide load time can be significant (2-5 seconds)
  • WebAssembly execution is generally fast but may be slower than native Python for some operations
  • Memory Usage: Large datasets and scientific computing operations may consume significant memory
  • Package Loading: Installing multiple packages can increase initialization time
  • Computation Speed: Complex mathematical operations may be slower than native Python execution
  • WebAssembly Constraints: Limited to packages compiled for WebAssembly/Pyodide environment
  • File System Access: No direct access to local file system, limited to in-memory operations
  • Network Restrictions: Limited network access, primarily through package installation
  • Package Availability: Not all Python packages are available or fully functional in Pyodide
  • Performance Overhead: WebAssembly execution may be slower than native Python for some operations
  • Memory Management: Limited by browser memory constraints and WebAssembly heap size
  • Dataset Size: Very large datasets may exceed memory limits or cause performance degradation
  • Serialization: Complex Python objects may not serialize properly for workflow data transfer
  • Execution Time: Long-running computations may be terminated by configured timeout limits

DOM: Document Object Model - Programming interface for web documents

CORS: Cross-Origin Resource Sharing - Security feature controlling cross-domain requests

CSP: Content Security Policy - Security standard preventing code injection attacks

Browser API: Programming interfaces provided by web browsers for extension functionality

Content Script: JavaScript code that runs in the context of web pages

Web Extraction: Automated extraction of data from websites

  • web extraction
  • browser automation
  • HTTP requests
  • DOM manipulation
  • content extraction
  • web interaction
  • “scrape”
  • “extract”
  • “fetch”
  • “get”
  • “browser”
  • “web”
  • “html”
  • “text”
  • “links”
  • “images”
  • “api”
  • data collection
  • web automation
  • content extraction
  • API integration
  • browser interaction
  • web extraction
  • Http-Request: Use when you need different approach to similar functionality
  • GetHTMLFromLink: Works well together in workflows
  • EditFields: Works well together in workflows
  • Filter: Works well together in workflows
  • GetHTMLFromLink → Code → EditFields: Common integration pattern
  • Code → Filter → DownloadAsFile: Common integration pattern

Decision Guides:

General Resources:

  • Migrated from JavaScript to Python execution using Pyodide WebAssembly runtime
  • Added support for scientific computing libraries (NumPy, Pandas, SciPy, Matplotlib)
  • Implemented package management system with micropip integration
  • Enhanced memory management and performance optimization for large datasets
  • 1.2.0: Added machine learning capabilities with scikit-learn integration
  • 1.1.0: Implemented Pyodide runtime and basic Python standard library support
  • 1.0.0: Initial release with JavaScript code execution (deprecated)

Last Updated: October 18, 2024 Tested With: Pyodide v0.24.1, Browser Extension v2.1.0 Validation Status: ✅ Python Examples Tested | ✅ Package Installation Verified | ✅ Performance Benchmarked