Pygaljs
Pygaljs is a Python package designed to provide static assets (JavaScript, CSS, fonts, etc.) for the pygal.js JavaScript charting library. It acts as a wrapper, making these web assets easily discoverable and servable within Python-based web applications. The current version is 1.0.2, and its release cadence is infrequent, with the last release in April 2020, indicating a stable but not actively developed state.
Common errors
-
ModuleNotFoundError: No module named 'pygaljs'
cause The `pygaljs` package is not installed in the active Python environment.fixRun `pip install pygaljs` in your terminal to install the package. -
Attempting to create a chart like: pygaljs.Line().add('series', [1, 2, 3]) results in AttributeError: module 'pygaljs' has no attribute 'Line'cause `pygaljs` is an asset package for a JavaScript library, not a Python charting API. It does not expose charting classes or functions directly.fixUse the `pygal` Python library (note: `pygal` not `pygaljs`) for generating SVG charts from Python, or use `pygal.js` (the JavaScript library) on the frontend if you are serving its assets via `pygaljs`.
Warnings
- gotcha Pygaljs provides assets for a JavaScript library (`pygal.js`), it is not a Python charting library itself. You cannot create charts directly by calling functions from `pygaljs` in Python.
- gotcha The exact subdirectory structure where `pygal.js` assets (JavaScript, CSS) are stored within the `pygaljs` package might not be immediately obvious or explicitly documented. This can lead to issues when configuring static file serving in web frameworks.
Install
-
pip install pygaljs
Imports
- pygaljs
import pygaljs
Quickstart
import os
import pygaljs
# The pygaljs package primarily provides static assets.
# Its main utility in Python code is often to determine the path
# to these assets for serving them in a web framework.
# Get the base directory of the installed pygaljs package
package_root = os.path.dirname(pygaljs.__file__)
print(f"Pygaljs package root: {package_root}")
# Example of how you might construct a path to a specific asset (e.g., JavaScript file)
# Note: The exact subdirectory structure depends on the pygal.js assets it bundles.
# A common pattern for such packages is to have a 'static' or 'dist' directory.
# This path might vary; typically, you'd look for a 'static' or 'dist' folder.
# For pygaljs, the assets are often directly under 'pygaljs/pygaljs/'.
# Checking the GitHub repository (https://github.com/ionelmc/python-pygaljs)
# shows assets like 'pygal-2.0.x.min.js' directly under 'pygaljs/pygaljs'.
pygaljs_assets_dir = os.path.join(package_root, 'pygaljs')
print(f"Pygaljs assets directory (potential): {pygaljs_assets_dir}")
# Verify if the directory exists and list some contents (for demonstration)
if os.path.exists(pygaljs_assets_dir):
print("Contents of pygaljs assets directory (first 5 files):")
for i, item in enumerate(os.listdir(pygaljs_assets_dir)):
if i >= 5: break
print(f" - {item}")
else:
print(f"Warning: {pygaljs_assets_dir} does not exist. Asset location might differ.")
# In a web framework (e.g., Flask, Django), you would configure
# a static file handler to serve files from `pygaljs_assets_dir`.
# For example, in Flask:
# app.static_folder = os.path.join(os.path.dirname(pygaljs.__file__), 'pygaljs')