Code
Overview
Section titled “Overview”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.
Python Execution Architecture
Section titled “Python Execution Architecture”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
Purpose and Functionality
Section titled “Purpose and Functionality”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
Key Features
Section titled “Key Features”- 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
Primary Use Cases
Section titled “Primary Use Cases”- 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
Parameters & Configuration
Section titled “Parameters & Configuration”Required Parameters
Section titled “Required Parameters”| Parameter | Type | Description | Example |
|---|---|---|---|
code | string | The Python code to execute | "import pandas as pd\nreturn df.describe()" |
Optional Parameters
Section titled “Optional Parameters”| Parameter | Type | Default | Description | Example |
|---|---|---|---|---|
timeout | number | 30000 | Maximum execution time in milliseconds | 10000 |
returnType | string | "auto" | Expected return type (auto, json, string, number) | "json" |
packages | array | [] | Additional Python packages to install via micropip | ["requests", "beautifulsoup4"] |
memoryLimit | string | "100MB" | Maximum memory usage for Python execution | "256MB" |
Advanced Configuration
Section titled “Advanced Configuration”{ "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" }}Pyodide Integration
Section titled “Pyodide Integration”Python Runtime
Section titled “Python Runtime”| Component | Purpose | Performance Impact |
|---|---|---|
Pyodide | WebAssembly-based Python runtime in browser | Near-native Python performance with initial load time |
micropip | Package installer for additional Python libraries | Network requests for package downloads |
Available Libraries
Section titled “Available Libraries”- 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
Cross-Browser Compatibility
Section titled “Cross-Browser Compatibility”| Feature | Chrome | Firefox | Safari | Edge |
|---|---|---|---|---|
| Pyodide Runtime | ✅ Full | ✅ Full | ✅ Full | ✅ Full |
| Scientific Libraries | ✅ Full | ✅ Full | ✅ Full | ✅ Full |
| Package Installation | ✅ Full | ✅ Full | ⚠️ Limited | ✅ Full |
| WebAssembly Support | ✅ Full | ✅ Full | ✅ Full | ✅ Full |
Security Considerations
Section titled “Security Considerations”- 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
Input/Output Specifications
Section titled “Input/Output Specifications”Input Data Structure
Section titled “Input Data Structure”{ "code": "string", "options": { "timeout": "number", "returnType": "string", "packages": "array", "memoryLimit": "string" }, "input_data": { "previousNodeData": "object", "workflowVariables": "object" }}Output Data Structure
Section titled “Output Data Structure”{ "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
Example 1: Data Analysis with Pandas
Section titled “Example 1: Data Analysis with Pandas”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
- Import required Python libraries (Pandas, NumPy)
- Extract text data from previous workflow node
- Process text using Python string methods and NumPy functions
- Calculate reading statistics with proper error handling
- 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_reportComplete Example: This code performs unsupervised machine learning analysis on extracted data, identifying patterns and clusters that can inform business decisions or further analysis.
Examples
Section titled “Examples”Basic Usage
Section titled “Basic Usage”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"}Advanced Usage
Section titled “Advanced Usage”This example shows more complex configuration options and integration patterns.
Configuration:
{ "parameter1": "advanced_value", "parameter2": false, "advancedOptions": { "option1": "value1", "option2": 100 }}Integration Example
Section titled “Integration Example”Example showing how this node integrates with other workflow nodes:
- Previous Node → Code → Next Node
- Data flows through the workflow with appropriate transformations
- Error handling and validation at each step
Integration Patterns
Section titled “Integration Patterns”Common Node Combinations
Section titled “Common Node Combinations”Pattern 1: Scientific Data Pipeline
Section titled “Pattern 1: Scientific Data Pipeline”- 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
Pattern 2: Text Processing & NLP
Section titled “Pattern 2: Text Processing & NLP”- 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
Best Practices
Section titled “Best Practices”- 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
Troubleshooting
Section titled “Troubleshooting”Common Issues
Section titled “Common Issues”Issue: Package Installation Failure
Section titled “Issue: Package Installation Failure”- Symptoms: Code node fails when trying to import or use additional packages
- Causes: Network connectivity issues, package compatibility, or incorrect package names
- Solutions:
- Verify package names are correct and available on PyPI
- Check network connectivity and firewall restrictions
- Use alternative packages or built-in libraries when possible
- Install packages one at a time to identify problematic dependencies
- Prevention: Test package installation in a separate environment first
Issue: Memory Limit Exceeded
Section titled “Issue: Memory Limit Exceeded”- Symptoms: Code execution fails with out-of-memory errors
- Causes: Large dataset processing, memory leaks, or inefficient algorithms
- Solutions:
- Increase memory limit in node configuration
- Process data in smaller chunks using generators or iterators
- Use more memory-efficient data structures and algorithms
- Clear variables and use garbage collection when processing large datasets
- Prevention: Profile memory usage during development and optimize accordingly
Pyodide-Specific Issues
Section titled “Pyodide-Specific Issues”Package Compatibility
Section titled “Package Compatibility”- Not all Python packages are available in Pyodide’s WebAssembly environment
- Some packages may have limited functionality compared to native Python installations
Performance Considerations
Section titled “Performance Considerations”- 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
Performance Issues
Section titled “Performance Issues”- 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
Limitations & Constraints
Section titled “Limitations & Constraints”Technical Limitations
Section titled “Technical Limitations”- 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
Pyodide Limitations
Section titled “Pyodide Limitations”- 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
Data Limitations
Section titled “Data Limitations”- 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
Key Terminology
Section titled “Key Terminology”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
Search & Discovery
Section titled “Search & Discovery”Keywords
Section titled “Keywords”- web extraction
- browser automation
- HTTP requests
- DOM manipulation
- content extraction
- web interaction
Common Search Terms
Section titled “Common Search Terms”- “scrape”
- “extract”
- “fetch”
- “get”
- “browser”
- “web”
- “html”
- “text”
- “links”
- “images”
- “api”
Primary Use Cases
Section titled “Primary Use Cases”- data collection
- web automation
- content extraction
- API integration
- browser interaction
- web extraction
Learning Path
Section titled “Learning Path”Skill Level: Beginner
Section titled “Skill Level: Beginner”Enhanced Cross-References
Section titled “Enhanced Cross-References”Workflow Patterns
Section titled “Workflow Patterns”Related Tutorials
Section titled “Related Tutorials”Practical Examples
Section titled “Practical Examples”Related Nodes
Section titled “Related Nodes”Similar Functionality
Section titled “Similar Functionality”- Http-Request: Use when you need different approach to similar functionality
Complementary Nodes
Section titled “Complementary Nodes”- GetHTMLFromLink: Works well together in workflows
- EditFields: Works well together in workflows
- Filter: Works well together in workflows
Common Workflow Patterns
Section titled “Common Workflow Patterns”- GetHTMLFromLink → Code → EditFields: Common integration pattern
- Code → Filter → DownloadAsFile: Common integration pattern
See Also
Section titled “See Also”- Browser Content Extraction
- Web Automation Patterns
- Multi-Node Automation
- Integration Patterns
- Browser Security Guide
Decision Guides:
General Resources:
Version History
Section titled “Version History”Current Version: 2.0.0
Section titled “Current Version: 2.0.0”- 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
Previous Versions
Section titled “Previous Versions”- 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)
Additional Resources
Section titled “Additional Resources”- Python Data Science Best Practices
- Scientific Computing with Pyodide
- Advanced Data Processing Patterns
- Machine Learning Workflows
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