Python API for Google Visualization

1.10.0 · active · verified Mon Apr 13

gviz-api is a helper Python library for developers implementing data sources for visualizations built on the Google Visualization API. It allows for creating `DataTable` objects in Python and serializing them into JSON string, JSON response, or JavaScript string formats consumable by client-side Google Charts. The current version is 1.10.0, released in October 2021, and the project appears to be stable but not frequently updated.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to define a table schema, populate it with data, and output the `DataTable` in different formats (JSON, JSON Response, JavaScript string) suitable for consumption by Google Charts.

import gviz_api
import datetime

# Define the schema of the table.
# The structure is: [(id, type, optional_label), ...]
description = [
    ("name", "string", "Meal"),
    ("diet", "string", "Diet Type"),
    ("calories", "number", "Calories"),
    ("healthy", "boolean", "Healthy?"),
    ("serving_date", "date", "Serving Date")
]

# Load the data. Data rows must match the schema order.
data = [
    ("Breakfast", "vegan", 250, True, datetime.date(2024, 4, 13)),
    ("Lunch", "vegetarian", 400, True, datetime.date(2024, 4, 13)),
    ("Dinner", "carnivore", 700, False, datetime.date(2024, 4, 12)),
    ("Snack", "omnivore", 150, True, datetime.date(2024, 4, 13)),
]

# Create a DataTable object.
data_table = gviz_api.DataTable(description)
data_table.AppendData(data)

# Output as JSON string (for embedding in a webpage)
json_string = data_table.ToJSon(columns_order=("name", "diet", "calories", "healthy", "serving_date"), order_by="calories")
print("JSON String:\n", json_string)

# Output as JSON response string (for a data source URL)
# req_id is often 0 for simple requests.
json_response = data_table.ToJSonResponse(columns_order=("name", "calories"), order_by="calories", req_id=0)
print("\nJSON Response (for data source):\n", json_response)

# Output as JavaScript string (for direct script injection, often for debugging)
javascript_string = data_table.ToJS(table_id="myDataTable", columns_order=("name", "calories"), order_by="calories")
print("\nJavaScript String (for embedding):\n", javascript_string)

view raw JSON →