plotjs 0.0.2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,40 @@
1
+ :root {
2
+ --default-opacity: 1;
3
+ --default-not-hovered-opacity: 0.2;
4
+ --default-transition: opacity 0.1s ease;
5
+ }
6
+
7
+ svg {
8
+ width: 100%;
9
+ height: auto;
10
+ }
11
+
12
+ .tooltip {
13
+ position: absolute;
14
+ background: #001d3d;
15
+ padding: 8px 12px;
16
+ border-radius: 6px;
17
+ color: #ffffff;
18
+ font-size: 14px;
19
+ box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
20
+ pointer-events: none;
21
+ display: none;
22
+ font-family: "Helvetica Neue", "Arial", sans-serif;
23
+ }
24
+
25
+ .plot-element {
26
+ opacity: var(--default-opacity);
27
+ transition: var(--default-transition);
28
+ }
29
+
30
+ .plot-element:hover {
31
+ opacity: var(--default-opacity);
32
+ }
33
+
34
+ .plot-element.not-hovered {
35
+ opacity: var(--default-not-hovered-opacity);
36
+ }
37
+
38
+ .plot-element.hovered {
39
+ opacity: var(--default-opacity);
40
+ }
plotjs/static/main.js ADDED
@@ -0,0 +1,143 @@
1
+ (function () {
2
+ class PlotSVGParser {
3
+ constructor(
4
+ svg,
5
+ tooltip,
6
+ tooltip_labels,
7
+ tooltip_groups,
8
+ show_tooltip,
9
+ tooltip_x_shift,
10
+ tooltip_y_shift
11
+ ) {
12
+ this.svg = svg;
13
+ this.tooltip = tooltip;
14
+ this.tooltip_labels = tooltip_labels;
15
+ this.tooltip_groups = tooltip_groups;
16
+ this.show_tooltip = show_tooltip;
17
+ this.tooltip_x_shift = tooltip_x_shift;
18
+ this.tooltip_y_shift = tooltip_y_shift;
19
+ }
20
+
21
+ find_bars(svg) {
22
+ // select all #patch
23
+ const bars = svg.selectAll('g[id^="patch"]').filter(function () {
24
+ const path = d3.select(this).select("path");
25
+
26
+ // that have a clip-path attribute
27
+ const clip = path.attr("clip-path");
28
+
29
+ // starting with "url("
30
+ return clip && clip.startsWith("url(");
31
+ });
32
+
33
+ bars.attr("class", "bar plot-element");
34
+ this.bars = bars;
35
+ return bars;
36
+ }
37
+
38
+ find_points(svg) {
39
+ const self = this;
40
+ // select all <use> in #PathCollection_1
41
+ const points = svg.selectAll('g[id^="PathCollection"] use');
42
+
43
+ points.attr("class", "point plot-element").each(function (_, i) {
44
+ d3.select(this).attr("data-group", self.tooltip_groups[i]);
45
+ });
46
+ this.points = points;
47
+ return points;
48
+ }
49
+
50
+ find_lines(svg) {
51
+ // select all <path> of Line2D elements
52
+ const lines = svg.selectAll('g[id^="line2d"] path').filter(function () {
53
+ return !this.closest('g[id^="matplotlib.axis"]');
54
+ });
55
+
56
+ lines.attr("class", "line plot-element");
57
+ this.lines = lines;
58
+ return lines;
59
+ }
60
+
61
+ find_areas(svg) {
62
+ // select all <path> of FillBetweenPolyCollection elements
63
+ const areas = svg.selectAll('g[id^="FillBetweenPolyCollection"] path');
64
+
65
+ areas.attr("class", "area plot-element");
66
+ this.areas = areas;
67
+ return areas;
68
+ }
69
+
70
+ setHoverEffect(plot_element) {
71
+ const self = this;
72
+ plot_element
73
+ .on("mouseover", function (event, d) {
74
+ const nodes = plot_element.nodes();
75
+ let i = nodes.indexOf(this);
76
+
77
+ const hovered_group = self.tooltip_groups[i];
78
+ plot_element.classed("not-hovered", true);
79
+ plot_element
80
+ .filter((_, j) => {
81
+ return self.tooltip_groups[j] === hovered_group;
82
+ })
83
+ .classed("not-hovered", false)
84
+ .classed("hovered", true);
85
+
86
+ tooltip
87
+ .style("display", self.show_tooltip)
88
+ .style("left", event.pageX + self.tooltip_x_shift + "px")
89
+ .style("top", event.pageY + self.tooltip_y_shift + "px")
90
+ .html(self.tooltip_labels[i]);
91
+ })
92
+ .on("mouseout", function () {
93
+ plot_element.classed("not-hovered", false).classed("hovered", false);
94
+ self.tooltip.style("display", "none");
95
+ });
96
+ }
97
+ }
98
+
99
+ const container = document.getElementById("{{ chart_id }}");
100
+ if (!container) return;
101
+
102
+ const tooltip = d3.select("#tooltip-{{ uuid }}");
103
+ const svg = d3.select(container).select("svg");
104
+
105
+ const plot_data = JSON.parse(`{{ plot_data_json | tojson | safe }}`);
106
+ const tooltip_labels = plot_data["tooltip_labels"];
107
+ const tooltip_groups = plot_data["tooltip_groups"];
108
+ const tooltip_x_shift = plot_data["tooltip_x_shift"];
109
+ const tooltip_y_shift = -plot_data["tooltip_y_shift"];
110
+
111
+ // no tooltip? no problem
112
+ if (tooltip_labels.length === 0 && tooltip_groups.length === 0) {
113
+ return; // Exit early
114
+ }
115
+ let show_tooltip;
116
+ if (tooltip_labels.length === 0 && tooltip_groups.length > 0) {
117
+ show_tooltip = "none";
118
+ } else {
119
+ show_tooltip = "block";
120
+ }
121
+
122
+ plotParser = new PlotSVGParser(
123
+ svg,
124
+ tooltip,
125
+ tooltip_labels,
126
+ tooltip_groups,
127
+ show_tooltip,
128
+ tooltip_x_shift,
129
+ tooltip_y_shift
130
+ );
131
+
132
+ // find all core plot elements
133
+ lines = plotParser.find_lines(svg);
134
+ bars = plotParser.find_bars(svg);
135
+ points = plotParser.find_points(svg);
136
+ areas = plotParser.find_areas(svg);
137
+
138
+ // give them the hover effect
139
+ plotParser.setHoverEffect(points);
140
+ plotParser.setHoverEffect(lines);
141
+ plotParser.setHoverEffect(bars);
142
+ plotParser.setHoverEffect(areas);
143
+ })();
@@ -0,0 +1,163 @@
1
+ {% set chart_id = "plot-container-" + uuid %}
2
+
3
+ <div id="{{ chart_id }}">
4
+ <style>
5
+ {{ default_css | safe }}
6
+ {{ additional_css | safe }}
7
+ </style>
8
+
9
+ {{ svg | safe }}
10
+ <div class="tooltip" id="tooltip-{{ uuid }}"></div>
11
+ </div>
12
+
13
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/7.9.0/d3.min.js"></script>
14
+ <script>
15
+ (function () {
16
+ class PlotSVGParser {
17
+ constructor(
18
+ svg,
19
+ tooltip,
20
+ tooltip_labels,
21
+ tooltip_groups,
22
+ show_tooltip,
23
+ tooltip_x_shift,
24
+ tooltip_y_shift
25
+ ) {
26
+ this.svg = svg;
27
+ this.tooltip = tooltip;
28
+ this.tooltip_labels = tooltip_labels;
29
+ this.tooltip_groups = tooltip_groups;
30
+ this.show_tooltip = show_tooltip;
31
+ this.tooltip_x_shift = tooltip_x_shift;
32
+ this.tooltip_y_shift = tooltip_y_shift;
33
+ }
34
+
35
+ find_bars(svg) {
36
+ // select all #patch
37
+ const bars = svg.selectAll('g[id^="patch"]').filter(function () {
38
+ const path = d3.select(this).select("path");
39
+
40
+ // that have a clip-path attribute
41
+ const clip = path.attr("clip-path");
42
+
43
+ // starting with "url("
44
+ return clip && clip.startsWith("url(");
45
+ });
46
+
47
+ bars.attr("class", "bar plot-element");
48
+ this.bars = bars;
49
+ return bars;
50
+ }
51
+
52
+ find_points(svg) {
53
+ const self = this;
54
+ // select all <use> in #PathCollection_1
55
+ const points = svg.selectAll('g[id^="PathCollection"] use');
56
+
57
+ points.attr("class", "point plot-element").each(function (_, i) {
58
+ d3.select(this).attr("data-group", self.tooltip_groups[i]);
59
+ });
60
+ this.points = points;
61
+ return points;
62
+ }
63
+
64
+ find_lines(svg) {
65
+ // select all <path> of Line2D elements
66
+ const lines = svg.selectAll('g[id^="line2d"] path').filter(function () {
67
+ return !this.closest('g[id^="matplotlib.axis"]');
68
+ });
69
+
70
+ lines.attr("class", "line plot-element");
71
+ this.lines = lines;
72
+ return lines;
73
+ }
74
+
75
+ find_areas(svg) {
76
+ // select all <path> of FillBetweenPolyCollection elements
77
+ const areas = svg.selectAll('g[id^="FillBetweenPolyCollection"] path');
78
+
79
+ areas.attr("class", "area plot-element");
80
+ this.areas = areas;
81
+ return areas;
82
+ }
83
+
84
+ setHoverEffect(plot_element) {
85
+ const self = this;
86
+ plot_element
87
+ .on("mouseover", function (event, d) {
88
+ const nodes = plot_element.nodes();
89
+ let i = nodes.indexOf(this);
90
+
91
+ const hovered_group = self.tooltip_groups[i];
92
+ plot_element.classed("not-hovered", true);
93
+ plot_element
94
+ .filter((_, j) => {
95
+ return self.tooltip_groups[j] === hovered_group;
96
+ })
97
+ .classed("not-hovered", false)
98
+ .classed("hovered", true);
99
+
100
+ tooltip
101
+ .style("display", self.show_tooltip)
102
+ .style("left", event.pageX + self.tooltip_x_shift + "px")
103
+ .style("top", event.pageY + self.tooltip_y_shift + "px")
104
+ .html(self.tooltip_labels[i]);
105
+ })
106
+ .on("mouseout", function () {
107
+ plot_element
108
+ .classed("not-hovered", false)
109
+ .classed("hovered", false);
110
+ self.tooltip.style("display", "none");
111
+ });
112
+ }
113
+ }
114
+
115
+ const container = document.getElementById("{{ chart_id }}");
116
+ if (!container) return;
117
+
118
+ const tooltip = d3.select("#tooltip-{{ uuid }}");
119
+ const svg = d3.select(container).select("svg");
120
+
121
+ const plot_data = JSON.parse(`{{ plot_data_json | tojson | safe }}`);
122
+ const tooltip_labels = plot_data["tooltip_labels"];
123
+ const tooltip_groups = plot_data["tooltip_groups"];
124
+ const tooltip_x_shift = plot_data["tooltip_x_shift"];
125
+ const tooltip_y_shift = -plot_data["tooltip_y_shift"];
126
+
127
+ let show_tooltip;
128
+ if (tooltip_labels.length === 0 && tooltip_groups.length === 0) {
129
+ show_tooltip = "none";
130
+ } else if (tooltip_labels.length === 0 && tooltip_groups.length > 0) {
131
+ show_tooltip = "none";
132
+ } else {
133
+ show_tooltip = "block";
134
+ }
135
+ console.log(show_tooltip);
136
+
137
+ plotParser = new PlotSVGParser(
138
+ svg,
139
+ tooltip,
140
+ tooltip_labels,
141
+ tooltip_groups,
142
+ show_tooltip,
143
+ tooltip_x_shift,
144
+ tooltip_y_shift
145
+ );
146
+
147
+ // find all core plot elements
148
+ lines = plotParser.find_lines(svg);
149
+ bars = plotParser.find_bars(svg);
150
+ points = plotParser.find_points(svg);
151
+ areas = plotParser.find_areas(svg);
152
+
153
+ // give them the hover effect
154
+ plotParser.setHoverEffect(points);
155
+ plotParser.setHoverEffect(lines);
156
+ plotParser.setHoverEffect(bars);
157
+ plotParser.setHoverEffect(areas);
158
+ })();
159
+ </script>
160
+ <script>
161
+ // prettier-ignore
162
+ {{ additional_javascript | safe }}
163
+ </script>
plotjs/utils.py ADDED
@@ -0,0 +1,30 @@
1
+ import narwhals.stable.v2 as nw
2
+ from narwhals.stable.v2.dependencies import is_numpy_array, is_into_series
3
+
4
+
5
+ def _vector_to_list(vector, name="labels and groups") -> list:
6
+ """
7
+ Function used to easily convert various kind of iterables to
8
+ lists in order to have standardised objects passed to javascript.
9
+
10
+ It accepts all backend series from narwhals and common objects
11
+ such as numpy arrays.
12
+
13
+ Todo: test this extensively to make sure it behaves as expected.
14
+
15
+ Args:
16
+ vector: A valid iterable.
17
+ name: The name passed to the error message when type is
18
+ invalid.
19
+
20
+ Returns:
21
+ A list
22
+ """
23
+ if isinstance(vector, (list, tuple)) or is_numpy_array(vector):
24
+ return list(vector)
25
+ elif is_into_series(vector):
26
+ return nw.from_native(vector, allow_series=True).to_list()
27
+ else:
28
+ raise ValueError(
29
+ f"{name} must be a Series or a valid iterable (list, tuple, ndarray...)."
30
+ )
@@ -0,0 +1,47 @@
1
+ Metadata-Version: 2.4
2
+ Name: plotjs
3
+ Version: 0.0.2
4
+ Summary: Bridge between static matplotlib and interactive storytelling
5
+ Author-email: Joseph Barbier <joseph.barbierdarnal@mail.com>
6
+ License-Expression: MIT
7
+ Project-URL: Homepage, https://y-sunflower.github.io/plotjs/
8
+ Project-URL: Issues, https://github.com/y-sunflower/plotjs/issues
9
+ Project-URL: Documentation, https://y-sunflower.github.io/plotjs/
10
+ Project-URL: Repository, https://github.com/y-sunflower/plotjs
11
+ Keywords: matplotlib,interactive,javascript,web,css,d3,mpld3,plotnine
12
+ Classifier: Programming Language :: Python :: 3
13
+ Classifier: Operating System :: OS Independent
14
+ Classifier: Development Status :: 3 - Alpha
15
+ Requires-Python: >=3.10
16
+ Description-Content-Type: text/markdown
17
+ License-File: LICENSE
18
+ Requires-Dist: jinja2>=3.0.0
19
+ Requires-Dist: matplotlib>=3.10.0
20
+ Requires-Dist: narwhals>=2.0.0
21
+ Dynamic: license-file
22
+
23
+ # `plotjs`: bridge between static matplotlib and interactive storytelling
24
+
25
+ `plotjs` is a proof of concept, inspired by [mpld3](https://github.com/mpld3/mpld3), to make matplotlib plots interactive (for the browser) with minimum user inputs.
26
+
27
+ The goal is also to give users a large customization power.
28
+
29
+ > Consider that the project is still **very unstable**.
30
+
31
+ [Online demo](https://y-sunflower.github.io/plotjs/)
32
+
33
+ <br><br>
34
+
35
+ ## Installation
36
+
37
+ From PyPI:
38
+
39
+ ```
40
+ pip install plotjs
41
+ ```
42
+
43
+ Latest dev version:
44
+
45
+ ```
46
+ pip install git+https://github.com/y-sunflower/plotjs.git
47
+ ```
@@ -0,0 +1,19 @@
1
+ plotjs/__init__.py,sha256=4L3ltmmCawwuJREKrJ_VuyiRlUaeHYrzTkJN6WDkmLc,93
2
+ plotjs/css.py,sha256=Xb5u5v6jbQxrwkgohbXdxdoLRXjHVE61hKIpNL4eT98,2402
3
+ plotjs/javascript.py,sha256=95MXk8D3hQzCQBrEBTogyCThkXGrno_hVQyjgc1_hZQ,488
4
+ plotjs/main.py,sha256=1TNku47gDU8lclbcnux3XoF8s6VP_tKXEE62rJFPfHw,8038
5
+ plotjs/utils.py,sha256=nz5rCdhos08lnnhYBaz6ZgYe7XaYA8ZyuMeeBWpi2Qo,974
6
+ plotjs/data/__init__.py,sha256=zyH9-p1G2B-6KQSQpsQLYpyzfrUa3phUc97a71FGGkM,152
7
+ plotjs/data/datasets.py,sha256=RtdRtrk-fAf3HpcVmc6MsuwR6ucyQU7hE7uvCgnmNpw,3448
8
+ plotjs/data/iris.csv,sha256=xSdC5QMVqZ-Vajg_rt91dVUmdfZAnvD5pHB23QhHmTA,3858
9
+ plotjs/data/mtcars.csv,sha256=t9kUtdR8c7PqWquOcpiIbXDG_fhzCH3w1KOCOT-BBls,1700
10
+ plotjs/data/titanic.csv,sha256=SkN_3gX-UmThcBpzh6xvt1OTdyuji7LJxWZAWvWvS9c,60302
11
+ plotjs/static/d3.min.js,sha256=8glLv2FBs1lyLE_kVOtsSw8OQswQzHr5IfwVj864ZTk,279706
12
+ plotjs/static/default.css,sha256=eSaoCCCwJKSSN4eU4WwAPHzvXX_hIGKM-TCIxENhTVI,729
13
+ plotjs/static/main.js,sha256=xRf-VGZhlKs9JDRUM-9mxckmZXkUd9F6ft9aMUfF_zw,4167
14
+ plotjs/static/template.html,sha256=eOmR8yCIx4DmH4ptoscS8JIKxNR9QWK0djNfcOdnejQ,4861
15
+ plotjs-0.0.2.dist-info/licenses/LICENSE,sha256=0tiC65L_9U5H0FD8LV6N8t8kI1iLDPVDLCK2qenV9w4,1071
16
+ plotjs-0.0.2.dist-info/METADATA,sha256=hcomZFuoZcRDw8G9hoBY6glisuQQHG0_Hid5BD4o9t0,1442
17
+ plotjs-0.0.2.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
18
+ plotjs-0.0.2.dist-info/top_level.txt,sha256=kDec-84OOKAI1HbXjkuHx3mOw0P2JDxW_tAbwuYscAc,7
19
+ plotjs-0.0.2.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (80.9.0)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Joseph Barbier
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1 @@
1
+ plotjs