Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a version of pygal.js that can be used as a plain JavaScript ES6 module #12

Open
wants to merge 27 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
3b29eb5
Copy __init__.js to pygal.js
tuzz Jan 7, 2024
bbfbb26
Remove the wrapping $builtinmodule function
tuzz Jan 7, 2024
f4eb270
Remove the Array.fill polyfill
tuzz Jan 7, 2024
9fdfa8f
Convert the Chart function to an ES6 class
tuzz Jan 7, 2024
8566679
Convert calls to createChartType to ES6 classes that extend Chart
tuzz Jan 7, 2024
885f5cb
Move the createChartType __init__ function into the Chart constructor
tuzz Jan 7, 2024
fd0a82b
Apply the list coercion logic from the createChartType #add function
tuzz Jan 7, 2024
6f5828e
Remove the dependency on Skulpt for the #some function
tuzz Jan 7, 2024
5666fce
Remove createChartType and kwfunc
tuzz Jan 7, 2024
673f71d
Remove unused variables: mod, KWARGS
tuzz Jan 7, 2024
1bd795b
Make all variables immutable
tuzz Jan 7, 2024
5429097
Preserve API of the old Skulpt implementation
tuzz Jan 7, 2024
c24b2db
Ensure super(…args) is called in all subclasses of Chart
tuzz Jan 7, 2024
d55c0b4
Allow charts to be constructed with no keyword arguments
tuzz Jan 7, 2024
b3adbdb
Provide an alternative way to set the global Skulpt configuration
tuzz Jan 7, 2024
6f8360c
Remove the dependency on jQuery
tuzz Jan 7, 2024
b0f3d85
Fix the chart-specific render functions not being applied
tuzz Jan 7, 2024
0521791
Rename demo.html -> demo-skulpt.html
tuzz Jan 7, 2024
a8fbce8
Add a demo of using the plain JavaScript file with Pyodide
tuzz Jan 7, 2024
6597cc4
Update the README
tuzz Jan 7, 2024
c396f7e
Remove the ‘v’ field from Chart constructor options
tuzz Jan 7, 2024
6721d68
Ensure all function arguments are JavaScript objects
tuzz Jan 7, 2024
1f5487f
Fix Pygal unable to plot ‘undefined’ values that should be 0
tuzz Jan 7, 2024
7221f0a
Ensure constructor arguments are forwarded for the exported charts
tuzz Jan 8, 2024
2a24450
Define getters and setters for chart properties
tuzz Jan 8, 2024
36f3e3d
Replace domOutput with a renderChart function
tuzz Jan 15, 2024
3b2e028
Extract the default values for width/height onto the config object
tuzz Jan 17, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 32 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ charting library driven by [Skulpt](http://www.skulpt.org/) and
## What is Skulpt?

> Python. Client side.
>
>
> Skulpt is an entirely in-browser implementation of Python.
>
>
> No preprocessing, plugins, or server-side support required, just write Python and reload.

## What is pygal?
Expand All @@ -25,13 +25,15 @@ charting library driven by [Skulpt](http://www.skulpt.org/) and

Pygal.js brings together the above projects to provide beatiful charts
created using python syntax all rendered in the browser with no server
necessary. Cool. See it in action on [trinket.io](https://trinket.io/charts).
necessary. Cool. See it in action on [trinket.io](https://trinket.io/charts).

New: You can now use pygal.js as a plain JavaScript file. See below for more details.

## Getting Started

Install JS dependencies with ```bower install```

Create a basic html page similar to DEMO.html.
Create a basic html page similar to demo-skulpt.html.

Add the pygal.js specific Skulpt configuration options

Expand Down Expand Up @@ -59,12 +61,37 @@ Sk.availableWidth = 600;
Sk.availableHeight = 400;
```

Note: If you use demo.html, you may need to serve it through an HTTP Server
Note: If you use demo-skulpt.html, you may need to serve it through an HTTP Server
due to the cross-site script URLs. "python -m SimpleHTTPServer" is one good
way.

Point your browser to your html page and have fun!

## Plain JavaScript file

You can now use Pygal as a plain JavaScript file if you want to call it from
JavaScript directly, or if you are using another Python interpreter such as
[Pyodide](https://pyodide.org/en/stable/).

The API is similar except there is no longer an `Sk` global. Instead, you will
need to set a `renderChart` function and `availableWidth` and `availableHeight`
values on the `pygal.config` object:

```javascript
import * as pygal from "./pygal.js"

pygal.config.renderChart = (chart) => Highcharts.chart(myElement, chart);
pygal.config.availableWidth = 600;
pygal.config.availableHeight = 400;
```

The plain JavaScript version also no longer requires jQuery. See demo-pyodide.html
for a working example using the plain JavaScript import.

Note that the plain JavaScript file is written for ES6 so you may need to
transpile it and add a polyfill for `Array.fill` if you want to support older
browsers.

## Opportunities for contribution

* Integration with a free and open-source javascript charting library rather
Expand Down
78 changes: 78 additions & 0 deletions demo-pyodide.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Pyodide</title>
<script src="https://cdn.jsdelivr.net/pyodide/v0.24.1/full/pyodide.js"></script>
<script src="https://code.highcharts.com/highcharts.js"></script>
</head>
<body>
<h3>Try This</h3>

<textarea id="input" cols="80" rows="30">

import pygal

# Our data is a list of series.
# Each series is a list with a label and a list of data points
data = [["Firefox", [None, None, 0, 16.6, 25, 31, 36.4, 45.5, 46.3, 42.8, 37.1]],
["Chrome", [None, None, None, None, None, None, 0, 3.9, 10.8, 23.8, 35.3]],
["IE", [85.8, 84.6, 84.7, 74.5, 66, 58.6, 54.7, 44.8, 36.2, 26.6, 20.1]],
["Others", [14.2, 15.4, 15.3, 8.9, 9, 10.4, 8.9, 5.8, 6.7, 6.8, 7.5]]]


# Make a Pygal chart
stackedline_chart = pygal.StackedLine(fill=True)
stackedline_chart.title = "Browser usage evolution (in %)"
stackedline_chart.x_labels = map(str, range(2002, 2013))

# For each series in our data, add label and data points
for label, data_points in data:
stackedline_chart.add(label, data_points)

# Render the chart
stackedline_chart.render()

# Example modified from the pygal.org documentation
</textarea>

<br/>
<button id="run" disabled>Run</button>

<pre id="info">Loading pyodide...</pre>
<div id="visual" style="width: fit-content;"></div>

<script type="module">
import * as pygal from "./pygal.js";

pygal.config.renderChart = (chart) => Highcharts.chart(visual, chart);

const runButton = document.getElementById("run");
const input = document.getElementById("input");
const info = document.getElementById("info");
const visual = document.getElementById("visual");

let pyodide;

const main = async () => {
pyodide = await loadPyodide();
await pyodide.registerJsModule("pygal", { ...pygal });

runButton.disabled = false;
info.innerHTML = "";
};

const run = async () => {
try {
await pyodide.loadPackagesFromImports(input.value);
await pyodide.runPython(input.value);
} catch (error) {
console.log(error);
}
};

main();
runButton.addEventListener("click", run);
</script>
</body>
</html>
File renamed without changes.
Loading