Python SDK Documentation

The official Python SDK for building data applications with Preswald.

Installation

Install the Preswald Python SDK using pip:

pip install preswald

For development installations with additional dependencies:

pip install preswald[dev]

Quick Start

Here's a simple example to get you started with Preswald:

import preswald as pw
import pandas as pd

# Initialize your app
app = pw.App("My Dashboard")

# Load some data
data = pd.DataFrame({
    'month': ['Jan', 'Feb', 'Mar', 'Apr', 'May'],
    'sales': [1000, 1200, 1100, 1300, 1500]
})

# Add a chart
app.add_chart(
    title="Monthly Sales",
    data=data,
    x="month",
    y="sales",
    chart_type="line"
)

# Run the app locally
app.run()

Authentication

To deploy your apps, you'll need to authenticate with your Preswald API key:

import preswald as pw

# Set your API key
pw.set_api_key("your-api-key-here")

# Or use environment variable
# export PRESWALD_API_KEY="your-api-key-here"

Creating Apps

The App class is the main entry point for creating Preswald applications:

# Create a new app
app = pw.App(
    title="Sales Dashboard",
    description="Monthly sales analytics",
    theme="light"  # or "dark"
)

# Add a sidebar
app.sidebar.add_filter(
    name="date_range",
    type="date_range",
    default=["2023-01-01", "2023-12-31"]
)

# Add multiple pages
app.add_page("Overview", overview_page)
app.add_page("Details", details_page)

Adding Charts

Preswald supports various chart types for data visualization:

# Line chart
app.add_chart(
    title="Revenue Trend",
    data=revenue_data,
    x="date",
    y="revenue",
    chart_type="line"
)

# Bar chart
app.add_chart(
    title="Sales by Region",
    data=sales_data,
    x="region",
    y="sales",
    chart_type="bar"
)

# Scatter plot
app.add_chart(
    title="Price vs Sales",
    data=product_data,
    x="price",
    y="sales",
    chart_type="scatter",
    color="category"
)

Deployment

Deploy your app to Preswald's cloud platform:

# Deploy your app
deployment = app.deploy(
    name="my-sales-dashboard",
    public=True,  # Make it publicly accessible
    custom_domain="dashboard.mycompany.com"  # Optional
)

print(f"App deployed at: {deployment.url}")