Introduction

Code Runner blocks are powerful tools in Hunch that allow you to execute custom Python or Bash code within your canvas. This feature enables you to process data, interact with external services, and create dynamic outputs, enhancing your AI workflows with custom logic and integrations.

Setting Up Your First Code Runner Block

Follow these steps to create and run your first Code Runner block:

  1. Create a new Canvas in Hunch.
  2. Change the initial Auto-AI Block into a Code Runner by selecting All -> Code Runner:
  3. Enter your code as a markdown code block, specifying the language as either python or bash:
  4. Click the ‘Run’ button to execute your code.

Simple Script Examples

Python Example

Here’s a simple Python script that outputs “Hello, World!”:

import hunch
hunch.output("Hello, World!")

Bash Example

Here’s a simple Bash script that achieves the same result:

echo "Hello, World!" > /output.md

Troubleshooting Guide

If you encounter issues with your Code Runner block, consider these common problems and solutions:

  1. Syntax Errors: Double-check your code for syntax errors. Use an IDE or online linter for assistance.
  2. Permissions: Ensure you have the necessary permissions for accessing resources or executing commands.
  3. Input/Output Handling: Verify that you’re using the correct methods for input and output (e.g., hunch.output() for Python, writing to /output.md for Bash).
  4. External Dependencies: When using external libraries in Python, confirm they are imported correctly and available in the environment.

Advanced Topics and Next Steps

Now that you’re familiar with the basics of Code Runner blocks, explore more advanced topics:

  • Working with complex inputs and data structures
  • Performing advanced data transformations
  • Integrating with external APIs and services
  • Optimizing code performance
  • Implementing error handling and logging best practices

Check out the other sections of this documentation for in-depth guidance on these topics and to unlock the full potential of Code Runner blocks in your Hunch workflows.