Python NVD3

0.16.0 · maintenance · verified Sat Apr 11

Python NVD3 is a Python wrapper for the NVD3 JavaScript library, which in turn builds re-usable charts and chart components for d3.js. It enables Python developers to generate interactive D3.js charts by writing Python code. The current version, 0.16.0, was uploaded to PyPI in April 2024. While receiving recent maintenance uploads, major feature development largely concluded around 2015, indicating a maintenance-focused release cadence.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a basic pie chart using `python-nvd3`. It initializes a `pieChart` object, adds data, builds the HTML and JavaScript content, and shows how to generate a complete HTML file ready for rendering in a web browser. Note that D3.js and NVD3.js JavaScript libraries, along with their CSS, must be included externally for the chart to render properly.

from nvd3 import pieChart

# Prepare data
xdata = ["Orange", "Banana", "Pear", "Kiwi", "Apple", "Strawberry", "Pineapple"]
ydata = [3, 4, 0, 1, 5, 7, 3]

# Create a pie chart
chart_name = 'pieChart'
chart = pieChart(name=chart_name, color_category='category20c', height=450, width=450)

# Add a series of data
extra_serie = {"tooltip": {"y_start": "", "y_end": " cal"}}
chart.add_serie(y=ydata, x=xdata, extra=extra_serie)

# Build the HTML content
chart.buildcontent()

# The generated HTML can be saved to a file or embedded in a web application
html_output = f"""
<!DOCTYPE html>
<html>
<head>
    <title>NVD3 Pie Chart</title>
    <!-- You MUST include D3.js and NVD3.js libraries -->
    <!-- For local testing, download from nvd3.org/bower_components/ -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.17/d3.min.js" charset="utf-8"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/nvd3/1.8.6/nv.d3.min.js" charset="utf-8"></script>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/nvd3/1.8.6/nv.d3.min.css" rel="stylesheet" type="text/css">
    <style>
        svg { font: 10px sans-serif; }
    </style>
</head>
<body>
    <h2>{chart_name}</h2>
    {chart.htmlcontent}
</body>
</html>
"""

# In a real application, you might pass this 'html_output' to a web framework template.
print(html_output)

# To demonstrate the generated chart in a browser, uncomment the following:
# with open('my_pie_chart.html', 'w') as f:
#     f.write(html_output)
# import webbrowser
# webbrowser.open('my_pie_chart.html')

view raw JSON →