Skip to main content

Python Execution & Visualization

Execute Python code for advanced data analysis, custom calculations, and create matplotlib visualizations.

Status

Production Ready - Full matplotlib & plotly support, interactive visualizations, file generation, and K8s-based execution

Overview

QRY's Python Execution feature allows you to run custom Python code for analysis that goes beyond SQL. Create static and interactive visualizations, perform statistical analysis, build models, and generate reports - all within your conversation.

The platform intelligently routes your code to the optimal execution environment and automatically captures both matplotlib (static) and plotly (interactive) visualizations.

How It Works

Automatic Detection

QRY automatically uses Python execution when you:

  • Request matplotlib charts or visualizations
  • Ask for statistical analysis (correlation, regression, etc.)
  • Mention Python libraries (pandas, numpy, scipy)
  • Need custom calculations

Manual Invocation

You can explicitly request Python:

Use Python to calculate the correlation matrix
Run this analysis in Python
Write Python code to create a heatmap

Available Libraries

Data Analysis

  • pandas: DataFrames and data manipulation
  • numpy: Numerical computing and arrays
  • scipy: Scientific computing and statistics

Visualization

  • matplotlib (v3.9.0): Static plots and charts
  • seaborn (v0.13.2): Statistical data visualization
  • plotly (v5.22.0): Interactive visualizations (full support)

Machine Learning

  • scikit-learn: ML algorithms and models
  • statsmodels: Statistical models

Utilities

  • datetime: Date and time handling
  • json: JSON processing
  • csv: CSV file operations
Custom Libraries

Need a library not listed? Contact your administrator to request installation.

Creating Visualizations

Static Charts (Matplotlib & Seaborn)

QRY automatically captures matplotlib figures - just use plt.show():

import matplotlib.pyplot as plt

# Simple line chart
plt.plot([1, 2, 3], [10, 20, 15])
plt.title('Revenue Trend')
plt.show() # Auto-captured and rendered inline

Natural language requests:

Create a line chart showing revenue over time using matplotlib
Use seaborn to create a correlation heatmap
Make a scatter plot of price vs quantity sold with a regression line
Matplotlib Best Practice

Use plt.show() to display charts - do not use plt.savefig(). The platform automatically captures figures and renders them as inline PNG images.

Interactive Charts (Plotly) ⭐ NEW

Create fully interactive visualizations with plotly - save as HTML and they're auto-captured:

import plotly.express as px

# Interactive scatter plot
df = px.data.iris()
fig = px.scatter(df, x='sepal_width', y='sepal_length',
color='species', hover_data=['petal_width'])
fig.write_html('chart.html') # Auto-captured with preview button

Natural language requests:

Create an interactive Sankey diagram showing customer flow
Make a plotly sunburst chart of sales by region and product
Build a 3D scatter plot to explore the relationship between price, quantity, and profit
Plotly Features

Interactive charts preserve full functionality:

  • Zoom & Pan - Explore specific regions
  • Hover tooltips - View detailed data points
  • Legend interactions - Show/hide series
  • Download - Export as PNG/SVG
  • Fullscreen preview - Opens in modal with iframe

Advanced Plotly Examples

Sankey Diagram (flow visualization):

import plotly.graph_objects as go

fig = go.Figure(data=[go.Sankey(
node = dict(label = ["A", "B", "C", "D"]),
link = dict(source = [0, 0, 1], target = [2, 3, 3], value = [8, 4, 2])
)])
fig.write_html('sankey.html')

Sunburst Chart (hierarchical data):

import plotly.express as px

df = px.data.tips()
fig = px.sunburst(df, path=['day', 'time', 'sex'], values='total_bill')
fig.write_html('sunburst.html')

Geographic Map:

import plotly.express as px

df = px.data.gapminder().query("year==2007")
fig = px.scatter_geo(df, locations="iso_alpha", size="pop",
hover_name="country", projection="natural earth")
fig.write_html('map.html')

Chart Customization

Matplotlib:

Create a bar chart with red bars, title "Monthly Sales", and x-axis label "Month"

Plotly:

Create an interactive line chart with custom colors and add range slider

Common Use Cases

Statistical Analysis

Correlation Analysis

Calculate the Pearson correlation between price and sales volume using Python

Regression Analysis

Perform linear regression to predict sales based on marketing spend

Hypothesis Testing

Run a t-test to compare sales between two regions

Data Transformations

Normalization

Normalize all numeric columns using min-max scaling

Feature Engineering

Create new features: day of week, month, and quarter from the date column

Outlier Detection

Use Python to identify and remove outliers using the IQR method

Time Series Analysis

Moving Averages

Calculate 7-day and 30-day moving averages for daily sales

Trend Analysis

Decompose the time series into trend, seasonal, and residual components

Forecasting

Use ARIMA to forecast next quarter's sales

Advanced Visualizations

Matplotlib Heatmaps

Create a correlation heatmap with annotations using seaborn

Multi-panel Figures

Create a 2x2 grid of subplots showing different views of the data

Custom Plots

Create a custom matplotlib figure with two y-axes

Interactive Plotly Charts

Create an interactive Sankey diagram showing customer journey from source to conversion
Build a 3D scatter plot showing the relationship between price, volume, and profit margin
Make an animated plotly chart showing sales trends over time by region

Working with Data

DataFrame Operations

Your query results are automatically available as a pandas DataFrame:

# QRY provides 'df' with your query results
df.head()
df.describe()
df.groupby('category').sum()

Data Access

Use Python to show summary statistics for all numeric columns
Calculate the median value for each category
Show the correlation between all pairs of variables

Data Cleaning

Remove rows where price is null or zero
Fill missing values in the quantity column with the median
Convert date strings to datetime objects

Generating Files

CSV Export

Export this data to CSV with custom formatting
Generate a CSV file with only the top 100 rows

JSON Export

Convert this data to JSON format and download

Excel Export

Create an Excel file with multiple sheets

Examples

Revenue Analysis with Chart

Show me monthly revenue for 2024 and create a line chart with matplotlib showing the trend

Result:

  • Data table with monthly revenue
  • Line chart PNG image
  • Downloadable chart file

Customer Segmentation

Group customers by total purchase amount into low, medium, high segments.
Create a bar chart showing count of customers in each segment.

Result:

  • Segmentation logic applied
  • Summary statistics
  • Bar chart visualization

Statistical Summary

For the sales data, calculate:
- Mean, median, standard deviation
- 25th, 50th, 75th percentiles
- Correlation with other variables
Create a box plot showing the distribution

Result:

  • Statistical measures
  • Box plot visualization

Time Series Forecast

Using the daily sales data, create a 30-day forecast with:
- Historical data as a line chart
- Forecast as a dashed line
- Confidence interval shaded

Result:

  • Forecast values
  • Visualization with historical and predicted data

Execution Environment

Intelligent Routing

QRY automatically routes your code to the optimal execution environment:

Gemini Executor (fast, inline):

  • Standard libraries: pandas, numpy, matplotlib, seaborn, scipy, sklearn
  • Dataset size < 2MB
  • Near-instant execution
  • Best for quick calculations and simple charts

Kubernetes Executor (powerful, flexible):

  • All Gemini libraries plus plotly (v5.22.0)
  • Automatically used when:
    • Code imports plotly (detected via import plotly or from plotly)
    • Dataset size > 2MB (Gemini's upload limit)
  • Takes 5-10 seconds to initialize
  • Handles large datasets (up to 1GB) and complex visualizations
Automatic Detection

You don't need to configure anything! The platform detects plotly imports and automatically routes to Kubernetes execution. Just write your code naturally.

Resource Limits

  • Memory: 2GB per execution
  • CPU Time: 300 seconds (5 minutes) for K8s, faster for Gemini
  • File Output: 100MB maximum per file
  • Dataset Size: Up to 1GB for Kubernetes executor
  • Disk Space: Temporary, cleared after execution

Security

  • Sandboxed: Code runs in isolated environments
  • No Network Access: Cannot make external API calls
  • Read-only Database: Cannot modify database
  • Temporary Storage: Files cleared after execution
  • Resource Limits: CPU, memory, and time limits enforced

Best Practices

Choosing Matplotlib vs Plotly

Use Matplotlib when:

  • You need standard statistical plots (line, bar, scatter, histogram)
  • Static images are sufficient
  • Fast rendering is important
  • Creating reports or documentation
  • Simple, straightforward visualizations

Use Plotly when:

  • You need advanced chart types (Sankey, Sunburst, Treemap, 3D)
  • User interaction adds value (zoom, pan, hover)
  • Exploring multi-dimensional data
  • Creating dashboards or presentations
  • Geographic or network visualizations

Code Efficiency

  1. Vectorized Operations: Use pandas/numpy instead of loops
  2. Memory Management: Avoid loading all data at once for very large datasets
  3. Early Filtering: Filter data before heavy computations
  4. Library Choice: Matplotlib for speed, Plotly for interactivity

Visualization Best Practices

For Matplotlib:

  • Use plt.show() only (never plt.savefig())
  • Add clear titles, axis labels, and legends
  • Use appropriate figure sizes: plt.figure(figsize=(10, 6))
  • Choose colorblind-friendly palettes

For Plotly:

  • Save with fig.write_html('filename.html')
  • Leverage hover data: hover_data=['col1', 'col2']
  • Use appropriate chart types (Sankey for flows, Sunburst for hierarchies)
  • Add interactive elements like range sliders and buttons

Error Handling

  1. Test Simple First: Start with basic analysis before complex code
  2. Check Data Types: Ensure columns are numeric before calculations
  3. Handle Nulls: Consider missing values in calculations
  4. Import Detection: Plotly imports trigger K8s routing automatically

Troubleshooting

Code Errors

Syntax Errors

  • Review the error message carefully
  • Ask QRY to fix the code: "The Python code has an error, please fix it"

Import Errors

  • Verify library is in supported list
  • Check spelling of library name
  • Request library installation from admin

Runtime Errors

  • Check for null/missing values in data
  • Verify data types are correct
  • Ensure operations are valid for data size

Performance Issues

Timeout Errors

  • Simplify the analysis
  • Reduce dataset size with filters
  • Break into smaller steps

Memory Errors

  • Filter to smaller subset of data
  • Use sampling for exploratory analysis
  • Avoid creating multiple large copies of data

Visualization Not Showing

For Matplotlib:

  • Ensure code calls plt.show() (not plt.savefig())
  • Check that figure has content to display
  • Verify data has values to plot

For Plotly:

  • Ensure code uses fig.write_html('filename.html')
  • Filename must end with .html
  • Check that plotly is imported correctly
  • HTML file size must be under 50MB

Advanced Features

Custom Functions

Define reusable functions:

Write a Python function to calculate customer lifetime value,
then apply it to all customers and show top 10

Multi-step Analysis

Chain multiple Python operations:

First, use Python to clean the data by removing outliers.
Then calculate correlation matrix.
Finally, create a heatmap of the correlations.

Machine Learning

Build a Random Forest model to predict customer churn.
Show feature importance as a bar chart.
Report model accuracy and confusion matrix.

Working with Multiple Visualizations

You can create multiple charts in a single execution:

import matplotlib.pyplot as plt
import plotly.express as px
import pandas as pd

# Assuming FILE_DATA contains your query results
df = pd.DataFrame(FILE_DATA)

# Create static matplotlib chart
plt.figure(figsize=(10, 5))
plt.subplot(1, 2, 1)
plt.hist(df['revenue'], bins=20)
plt.title('Revenue Distribution')

plt.subplot(1, 2, 2)
plt.bar(df['category'], df['count'])
plt.title('Category Counts')
plt.show()

# Create interactive plotly chart
fig = px.scatter(df, x='revenue', y='profit',
color='category', size='count',
hover_data=['product_name'],
title='Revenue vs Profit Analysis')
fig.write_html('interactive_analysis.html')

OUTPUT = df.describe().to_dict()

Result:

  • 2 matplotlib charts (rendered inline as PNG)
  • 1 interactive plotly chart (preview button)
  • Statistical summary in OUTPUT

File Downloads

Generated files are automatically available for download:

  • Static Charts: PNG images from matplotlib/seaborn (inline)
  • Interactive Charts: HTML files from plotly (preview button + download)
  • Data: CSV, JSON, Excel exports
  • Reports: Text files and summaries

Files appear as download links in the conversation. Interactive HTML charts can be opened in fullscreen preview or downloaded for sharing.


Combine Features

Python execution works great with File Upload - upload a CSV and run Python analysis without database import!

QRYA product of IXEN.