What is the Code Block?
The Code block lets you write Python code to manipulate workflow data. Use it when you need custom logic, complex transformations, or data processing that goes beyond what other blocks can do. Common use cases:- Transform and filter data
- Parse and extract information from text
- Perform calculations and aggregations
- Combine data from multiple steps
- Format data for specific outputs
- Create custom data structures
- Clean and validate data
How Does the Code Block Work?
- Write Python code directly in the block
- Access previous steps using
step_1,step_2, etc. - Process the data using Python and available libraries
- Return the result using
returnstatement - Output becomes available to subsequent steps
Configuration
Language
Currently supports: PythonCode (Required)
Write your Python code in this field. You don’t need to define a function - just write the code and usereturn to output the result.
Basic example:
Available Libraries
The Code block includes these Python libraries:- BeautifulSoup - HTML/XML parsing
- numpy (np) - Numerical computations
- pandas (pd) - Data analysis and manipulation
- json - JSON encoding/decoding
- datetime - Date and time handling
- math - Mathematical functions
- random - Random number generation
- re - Regular expressions
- collections - Container data types
- markdown - Markdown processing
- lxml - XML processing
Accessing Workflow Data
Previous Steps
Access output from previous steps usingstep_X notation:
Input Variables
Access workflow inputs:Use Cases and Examples
Use Case 1: Extract Keywords from Array
Input from Related Keywords block:Use Case 2: Filter High-Volume Keywords
Input:Use Case 3: Calculate Statistics
Input:Use Case 4: Parse and Extract from Text
Input: Text with URLs Code:Use Case 5: Create Structured Data
Input: Array of keywords Code:Use Case 6: Format Data for Export
Input: Keyword data Code:Use Case 7: Clean and Normalize Data
Input: Data with inconsistent formatting Code:Use Case 8: Group and Aggregate Data
Input: Keywords with categories Code:Use Case 9: Parse HTML Content
Input: HTML content from web scraping Code:Use Case 10: Generate Date Ranges
Code:Use Case 11: Sort and Rank Data
Input: Keywords array Code:Use Case 12: Using Pandas for Data Analysis
Input: Keyword data array Code:Use Case 13: Filter by Multiple Conditions
Input: Keywords with multiple metrics Code:Use Case 14: Create Summary Report
Input: Multiple steps with different data Code:Best Practices
Code Organization
- Keep it simple: Write clear, readable code
- Comment complex logic: Use
#for comments - One responsibility: Each code block should do one thing well
- Test incrementally: Start simple, add complexity gradually
Data Handling
- Validate input: Check if data exists before processing
- Handle errors: Use try/except for operations that might fail
- Convert types: Ensure data types are correct (int, float, str)
- Return meaningful data: Structure output for downstream blocks
Performance
- Avoid unnecessary loops: Use list comprehensions when possible
- Filter early: Reduce data size before complex operations
- Limit iterations: Maximum 25 loops allowed
- Be efficient: Use built-in functions and libraries
Common Patterns
Safe data access:Debugging
Using Print Statements
Useprint() to log information during execution:
Limitations
Security Restrictions
These modules are blocked for security:os- Operating system interfacesys- System-specific parameterssubprocess- Subprocess managementshutil- File operationssocket- Network connectionsmultiprocessing- Process-based parallelism
Loop Limits
Maximum of 25 loops allowed per code block execution. If you need more iterations, consider:- Processing data in batches with multiple code blocks
- Using the Loop block for iteration
- Simplifying your logic
Troubleshooting
Code Not Returning Output
Problem: Block executes but output is empty. Solution:- Make sure you use
returnstatement - Check your return value is not
None - Verify data structure is correct
KeyError: Step or Property Not Found
Problem: Error accessing step data. Solution:- Check step number is correct
- Verify previous step completed successfully
- Use safe access patterns
Type Errors
Problem: Operations fail due to wrong data types. Solution:- Convert strings to numbers explicitly
- Check data types before operations
- Handle missing or null values
Loop Limit Exceeded
Problem: Code exceeds 25 loop limit. Solution:- Reduce iterations
- Use list comprehensions (more efficient)
- Use Loop block for large iterations
Error Handling
The Code block will fail if:- Python syntax is incorrect
- Referenced data doesn’t exist
- Loop limit is exceeded
- Blocked modules are imported
- Execution times out
- Terminate Workflow: Stop execution
- Continue Execution: Proceed with
Noneoutput
What’s Next
Now that you understand the Code block:- Format output with Text Block
- Convert to JSON with Convert to JSON
- Process data in bulk with Loop Block
- Save results to Google Sheets
- Learn about Liquid Templating
- Learn about Variable Referencing