n8n's visual workflow builder is powerful, but some tasks need custom code. Here is how to combine the best of both worlds—no-code simplicity with Python's unlimited flexibility.
Why Combine Python and n8n?
n8n Strengths
- Visual workflow design
- Easy app integrations
- Trigger and scheduling built-in
- Non-developers can understand flows
Python Strengths
- Complex data transformation
- Advanced algorithms
- Machine learning integration
- Unlimited customization
Together
n8n handles orchestration and integrations; Python handles heavy processing.
Methods for Integration
Method 1: Code Node
n8n has a built-in Code node that runs JavaScript. For Python, you can:
- Use a Python-to-JS transpiler
- Call Python via shell commands
- Use Pyodide (Python in browser)
Method 2: HTTP Request to Python API
Best approach for complex Python logic:
- Build a simple Python API (Flask/FastAPI)
- Host it (server, serverless, Docker)
- n8n calls the API via HTTP Request node
- Python processes and returns results
Method 3: Execute Command Node
If n8n and Python are on the same server:
- Use Execute Command node
- Call Python script directly
- Pass data via arguments or files
- Read output back into n8n
Example: Data Processing Pipeline
The Workflow
- n8n: Trigger on new file upload
- n8n: Download file from cloud storage
- Python API: Process file (complex transformations)
- n8n: Take processed data
- n8n: Update CRM, send notifications
Python API Example (FastAPI)
from fastapi import FastAPI
import pandas as pd
app = FastAPI()
@app.post("/process-data")
async def process_data(data: dict):
# Convert to DataFrame
df = pd.DataFrame(data['records'])
# Complex processing
df['total'] = df['quantity'] * df['price']
summary = df.groupby('category')['total'].sum()
return {"summary": summary.to_dict()}
n8n HTTP Request
- Method: POST
- URL: http://your-api/process-data
- Body: JSON with your data
Use Cases for This Combo
- ML predictions: n8n triggers, Python model predicts
- Document processing: n8n orchestrates, Python parses
- Data enrichment: n8n collects, Python transforms
- Report generation: n8n schedules, Python generates
Deployment Options
Same Server
- Simplest setup
- Use Execute Command or local API
- Good for low volume
Separate Services
- Python API on separate server or serverless
- Better scaling and maintenance
- n8n connects via HTTP
Docker Compose
- n8n and Python API in containers
- Easy deployment and scaling
- Network communication between containers
0 comments