graph_widget 0.1.0__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.
- graph_widget/__init__.py +0 -0
- graph_widget/dev.py +137 -0
- graph_widget/graph.css +3 -0
- graph_widget/graph.js +230 -0
- graph_widget-0.1.0.dist-info/METADATA +16 -0
- graph_widget-0.1.0.dist-info/RECORD +7 -0
- graph_widget-0.1.0.dist-info/WHEEL +4 -0
graph_widget/__init__.py
ADDED
|
File without changes
|
graph_widget/dev.py
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import marimo
|
|
2
|
+
|
|
3
|
+
__generated_with = "0.11.6"
|
|
4
|
+
app = marimo.App(width="medium")
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
@app.cell
|
|
8
|
+
def _():
|
|
9
|
+
import marimo as mo
|
|
10
|
+
import pandas as pd
|
|
11
|
+
return mo, pd
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
@app.cell
|
|
15
|
+
def _():
|
|
16
|
+
import anywidget
|
|
17
|
+
import traitlets
|
|
18
|
+
|
|
19
|
+
class ForceGraphWidget(anywidget.AnyWidget):
|
|
20
|
+
_esm = "./src/graph_widget/graph.js"
|
|
21
|
+
_css = "./src/graph_widget/graph.css"
|
|
22
|
+
data = traitlets.Dict().tag(sync=True)
|
|
23
|
+
repulsion = traitlets.Int().tag(sync=True)
|
|
24
|
+
node_scale = traitlets.Int().tag(sync=True)
|
|
25
|
+
colour_feature = traitlets.Unicode().tag(sync=True)
|
|
26
|
+
selected_ids = traitlets.List([]).tag(sync=True)
|
|
27
|
+
return ForceGraphWidget, anywidget, traitlets
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
@app.cell
|
|
31
|
+
def _():
|
|
32
|
+
import json
|
|
33
|
+
import requests
|
|
34
|
+
|
|
35
|
+
url = "https://raw.githubusercontent.com/observablehq/sample-datasets/refs/heads/main/miserables.json"
|
|
36
|
+
response = requests.get(url)
|
|
37
|
+
data = response.json()
|
|
38
|
+
return data, json, requests, response, url
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
@app.cell
|
|
42
|
+
def _(data, pd):
|
|
43
|
+
df = pd.DataFrame(data["nodes"])
|
|
44
|
+
return (df,)
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
@app.cell
|
|
48
|
+
def _(df):
|
|
49
|
+
df
|
|
50
|
+
return
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
@app.cell
|
|
54
|
+
def _(mo):
|
|
55
|
+
repulsion_slider = mo.ui.slider(
|
|
56
|
+
start=-200, stop=10000, step=10, value=1, debounce=False, label="Repulsion"
|
|
57
|
+
)
|
|
58
|
+
node_scale_slider = mo.ui.slider(
|
|
59
|
+
start=1, stop=500, step=1, value=20, debounce=True, label="Node scale"
|
|
60
|
+
)
|
|
61
|
+
return node_scale_slider, repulsion_slider
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
@app.cell
|
|
65
|
+
def _(data, mo):
|
|
66
|
+
colour_feature_selector = mo.ui.dropdown(
|
|
67
|
+
list(data["nodes"][0].keys()), value="id", label="Colour feature"
|
|
68
|
+
)
|
|
69
|
+
return (colour_feature_selector,)
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
@app.cell
|
|
73
|
+
def _(
|
|
74
|
+
ForceGraphWidget,
|
|
75
|
+
colour_feature_selector,
|
|
76
|
+
data,
|
|
77
|
+
mo,
|
|
78
|
+
node_scale_slider,
|
|
79
|
+
repulsion_slider,
|
|
80
|
+
):
|
|
81
|
+
data_graph = mo.ui.anywidget(
|
|
82
|
+
ForceGraphWidget(
|
|
83
|
+
data=data,
|
|
84
|
+
repulsion=repulsion_slider.value,
|
|
85
|
+
node_scale=node_scale_slider.value,
|
|
86
|
+
colour_feature=colour_feature_selector.value,
|
|
87
|
+
)
|
|
88
|
+
)
|
|
89
|
+
return (data_graph,)
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
@app.cell
|
|
93
|
+
def _(
|
|
94
|
+
colour_feature_selector,
|
|
95
|
+
data_graph,
|
|
96
|
+
mo,
|
|
97
|
+
node_scale_slider,
|
|
98
|
+
repulsion_slider,
|
|
99
|
+
):
|
|
100
|
+
plot = mo.hstack([ data_graph,
|
|
101
|
+
mo.vstack([
|
|
102
|
+
repulsion_slider,
|
|
103
|
+
node_scale_slider,
|
|
104
|
+
colour_feature_selector])])
|
|
105
|
+
return (plot,)
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
@app.cell
|
|
109
|
+
def _(plot):
|
|
110
|
+
plot
|
|
111
|
+
return
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
@app.cell
|
|
115
|
+
def _(data_graph):
|
|
116
|
+
selected = data_graph.selected_ids
|
|
117
|
+
return (selected,)
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
@app.cell
|
|
121
|
+
def _(selected):
|
|
122
|
+
selected
|
|
123
|
+
return
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
@app.cell
|
|
127
|
+
def _():
|
|
128
|
+
return
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
@app.cell
|
|
132
|
+
def _():
|
|
133
|
+
return
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
if __name__ == "__main__":
|
|
137
|
+
app.run()
|
graph_widget/graph.css
ADDED
graph_widget/graph.js
ADDED
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
import ForceGraph from "https://esm.sh/force-graph";
|
|
2
|
+
import { select } from "https://esm.sh/d3-selection";
|
|
3
|
+
import { extent } from "https://esm.sh/d3-array";
|
|
4
|
+
import { brush } from "https://esm.sh/d3-brush@3";
|
|
5
|
+
import { forceManyBody } from "https://esm.sh/d3";
|
|
6
|
+
import { scaleLinear, scaleOrdinal, scaleIdentity } from "https://esm.sh/d3-scale";
|
|
7
|
+
import { schemeCategory10 } from "https://esm.sh/d3-scale-chromatic";
|
|
8
|
+
import RBush from 'https://cdn.jsdelivr.net/npm/rbush/+esm';
|
|
9
|
+
|
|
10
|
+
let width = "800";
|
|
11
|
+
let height = "500";
|
|
12
|
+
|
|
13
|
+
class MyRBush extends RBush {
|
|
14
|
+
toBBox(node) { return { id: node.id, minX: node.x, minY: node.y, maxX: node.x, maxY: node.y }; }
|
|
15
|
+
compareMinX(a, b) { return a.x - b.x; }
|
|
16
|
+
compareMinY(a, b) { return a.y - b.y; }
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
let local_selected_ids = []
|
|
20
|
+
let repulsion = 1;
|
|
21
|
+
let node_scale = 1;
|
|
22
|
+
let plot;
|
|
23
|
+
let tree;
|
|
24
|
+
let node_size = 5;
|
|
25
|
+
let colour_feature = undefined;
|
|
26
|
+
let colour_scale;
|
|
27
|
+
|
|
28
|
+
function debounce(func, wait) {
|
|
29
|
+
let timeout;
|
|
30
|
+
return function (...args) {
|
|
31
|
+
clearTimeout(timeout);
|
|
32
|
+
timeout = setTimeout(() => func.apply(this, args), wait);
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const create_rtree = (data) => {
|
|
37
|
+
tree = new MyRBush()
|
|
38
|
+
tree.load(data)
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function create_plot(el, width, height, data) {
|
|
42
|
+
return ForceGraph()(el)
|
|
43
|
+
.width(width)
|
|
44
|
+
.height(height)
|
|
45
|
+
.graphData(data)
|
|
46
|
+
// .d3Force('charge',forceManyBody().strength(-repulsion))
|
|
47
|
+
.cooldownTime(2000)
|
|
48
|
+
.minZoom(0.001)
|
|
49
|
+
.onEngineStop(() => {
|
|
50
|
+
plot.zoomToFit(400);
|
|
51
|
+
create_rtree(data["nodes"]);
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const get_feature_type = (value) => {
|
|
56
|
+
if (typeof value === 'number') return 'numeric'; // Check for numeric
|
|
57
|
+
if (typeof value === 'string' && value.startsWith('#')) return 'hash_string'; // Check for strings starting with #
|
|
58
|
+
return 'categorical'; // Default to categorical if not numeric or hash_string
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// function get_colour_scale(data, colour_feature) {
|
|
62
|
+
// const feature_values = nodes.map(node => node[colour_feature])
|
|
63
|
+
// feature_type = get_feature_type(feature_values[0])
|
|
64
|
+
// if (feature_type == 'numeric') {
|
|
65
|
+
// const domain = extent(feature_values)
|
|
66
|
+
// colour_scale = scaleLinear()
|
|
67
|
+
// .domain(domain)
|
|
68
|
+
// .range(["yellow", "blue"])
|
|
69
|
+
// } else if (feature_type == 'categorical') {
|
|
70
|
+
// const domain = extent(feature_values)
|
|
71
|
+
// colour_scale = scaleOrdinal()
|
|
72
|
+
// .domain(domain)
|
|
73
|
+
// .range(schemeCategory10);
|
|
74
|
+
// } else if (feature_type == 'hash_string') {
|
|
75
|
+
// colour_scale = scaleIdentity()
|
|
76
|
+
// }
|
|
77
|
+
// }
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
function render({ model, el }) {
|
|
81
|
+
const debouncedSaveChanges = debounce(() => model.save_changes(), 300);
|
|
82
|
+
|
|
83
|
+
const data = model.get("data");
|
|
84
|
+
repulsion = model.get("repulsion");
|
|
85
|
+
let global_selected_ids = model.get("selected_ids")
|
|
86
|
+
|
|
87
|
+
plot = create_plot(el, width, height, data);
|
|
88
|
+
|
|
89
|
+
model.on("change:repulsion", () => {
|
|
90
|
+
repulsion = model.get("repulsion");
|
|
91
|
+
plot.d3Force('charge', forceManyBody().strength(-repulsion));
|
|
92
|
+
plot.d3ReheatSimulation();
|
|
93
|
+
});
|
|
94
|
+
model.on("change:node_scale", () => {
|
|
95
|
+
node_size = model.get("node_scale");
|
|
96
|
+
plot.nodeRelSize(node_size * node_scale)
|
|
97
|
+
});
|
|
98
|
+
model.on("change:colour_feature", () => {
|
|
99
|
+
colour_feature = model.get("colour_feature");
|
|
100
|
+
let feature_values = data.nodes.map(node => node[colour_feature])
|
|
101
|
+
let feature_type = get_feature_type(feature_values[0])
|
|
102
|
+
// let colour_scale = undefined;
|
|
103
|
+
colour_scale = ( feature_type == 'numeric')
|
|
104
|
+
? scaleLinear().domain(extent(feature_values)).range(["yellow", "blue"])
|
|
105
|
+
: ( feature_type == 'categorical')
|
|
106
|
+
? colour_scale = scaleOrdinal().domain(extent(feature_values)).range(schemeCategory10)
|
|
107
|
+
: ( feature_type == 'hash_string')
|
|
108
|
+
? colour_scale = scaleIdentity()
|
|
109
|
+
: undefined
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
// if ( feature_type == 'numeric') {
|
|
113
|
+
// colour_scale = scaleLinear().domain(extent(feature_values)).range(["yellow", "blue"])
|
|
114
|
+
// } else if ( feature_type == 'categorical') {
|
|
115
|
+
// colour_scale = scaleOrdinal().domain(extent(feature_values)).range(schemeCategory10);
|
|
116
|
+
// } else if ( feature_type == 'hash_string') {
|
|
117
|
+
// colour_scale = scaleIdentity()
|
|
118
|
+
// }
|
|
119
|
+
|
|
120
|
+
plot.nodeColor((d) => {
|
|
121
|
+
return global_selected_ids.includes(d.id)
|
|
122
|
+
? "yellow"
|
|
123
|
+
: local_selected_ids.includes(d.id)
|
|
124
|
+
? "rgba(255,0,0,0.5)" // Red for local_selected_ids nodes
|
|
125
|
+
: feature_type === undefined
|
|
126
|
+
? "steelblue" // Default colour
|
|
127
|
+
: feature_type === 'hash_string'
|
|
128
|
+
? d[colour_feature] // Use the hash string directly
|
|
129
|
+
: colour_scale(d[colour_feature])})
|
|
130
|
+
});
|
|
131
|
+
model.on("change:selected_ids", () => {
|
|
132
|
+
global_selected_ids = model.get("selected_ids");
|
|
133
|
+
plot.nodeColor((d) =>
|
|
134
|
+
global_selected_ids.includes(d.id)
|
|
135
|
+
? "red"
|
|
136
|
+
: local_selected_ids.includes(d.id)
|
|
137
|
+
? "rgba(255,0,0,0.5)"
|
|
138
|
+
: d["colour_hex"],
|
|
139
|
+
);
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
// model.on("change:selected_nodes", () => {
|
|
143
|
+
// local_selected_ids = model.get("selected_nodes");
|
|
144
|
+
// plot.nodeColor((d) =>
|
|
145
|
+
// global_selected_ids.includes(d.id)
|
|
146
|
+
// ? "red"
|
|
147
|
+
// : selected.includes(d.id)
|
|
148
|
+
// ? "rgba(255,0,0,0.5)"
|
|
149
|
+
// : d["colour_hex"],
|
|
150
|
+
// );
|
|
151
|
+
// });
|
|
152
|
+
|
|
153
|
+
let brush_active = false;
|
|
154
|
+
|
|
155
|
+
let container = el.querySelector(".force-graph-container")
|
|
156
|
+
|
|
157
|
+
// Create an overlay for brushing
|
|
158
|
+
let overlay = select(container)
|
|
159
|
+
.append("svg")
|
|
160
|
+
.attr("id", "overlay")
|
|
161
|
+
.style("position", "absolute")
|
|
162
|
+
.style("top", 0)
|
|
163
|
+
.style("left", 0)
|
|
164
|
+
.style("width", "100%")
|
|
165
|
+
.style("height", "100%")
|
|
166
|
+
.style("pointer-events", "none")
|
|
167
|
+
|
|
168
|
+
let activate_brush = () => {
|
|
169
|
+
brush_active = true;
|
|
170
|
+
el.style.pointerEvents = "none"
|
|
171
|
+
overlay.style.pointerEvents = "auto"
|
|
172
|
+
|
|
173
|
+
if (!overlay.select("#brush_group").empty()) return;
|
|
174
|
+
overlay.insert("g", ":first-child")
|
|
175
|
+
.attr("id", "brush_group")
|
|
176
|
+
.attr("class", "brush")
|
|
177
|
+
.call(my_brush)
|
|
178
|
+
}
|
|
179
|
+
let disactivate_brush = () => {
|
|
180
|
+
brush_active = false;
|
|
181
|
+
el.style.pointerEvents = "auto"
|
|
182
|
+
overlay.style.pointerEvents = "none"
|
|
183
|
+
|
|
184
|
+
overlay.select("#brush_group").remove();
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
let brushed = (event) => {
|
|
188
|
+
if (event.selection) {
|
|
189
|
+
brush_active = true;
|
|
190
|
+
let [[x0_screen, y0_screen], [x1_screen, y1_screen]] =
|
|
191
|
+
event.selection;
|
|
192
|
+
let corner0 = plot.screen2GraphCoords(x0_screen, y0_screen);
|
|
193
|
+
let corner1 = plot.screen2GraphCoords(x1_screen, y1_screen);
|
|
194
|
+
let bbox = {
|
|
195
|
+
minX: Math.min(corner0.x, corner1.x),
|
|
196
|
+
minY: Math.min(corner0.y, corner1.y),
|
|
197
|
+
maxX: Math.max(corner0.x, corner1.x),
|
|
198
|
+
maxY: Math.max(corner0.y, corner1.y)
|
|
199
|
+
};
|
|
200
|
+
let selectedNodes = tree.search(bbox)
|
|
201
|
+
local_selected_ids = selectedNodes.map(node => node.id);
|
|
202
|
+
model.set("selected_ids", local_selected_ids);
|
|
203
|
+
console.log(local_selected_ids)
|
|
204
|
+
debouncedSaveChanges();
|
|
205
|
+
}
|
|
206
|
+
};
|
|
207
|
+
|
|
208
|
+
let my_brush = brush()
|
|
209
|
+
.filter((event) => {
|
|
210
|
+
// Prevent contextual menu on Ctrl-click (macOS)
|
|
211
|
+
if (event.ctrlKey && event.button === 0) {
|
|
212
|
+
event.preventDefault();
|
|
213
|
+
}
|
|
214
|
+
return (
|
|
215
|
+
!event.button && // Ignore mouse buttons other than left-click
|
|
216
|
+
(event.metaKey || event.ctrlKey || event.target.__data__.type !== "overlay")
|
|
217
|
+
)
|
|
218
|
+
})
|
|
219
|
+
.extent([[0, 0], [width, height]])
|
|
220
|
+
.on("start brush end", brushed);
|
|
221
|
+
|
|
222
|
+
window.addEventListener("keydown", (e) => {
|
|
223
|
+
if (e.metaKey && !brush_active) { activate_brush() }
|
|
224
|
+
})
|
|
225
|
+
window.addEventListener("dblclick", (e) => {
|
|
226
|
+
if (brush_active) { disactivate_brush() }
|
|
227
|
+
})
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
export default { render };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: graph_widget
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Vasturiano graph as an anywidget
|
|
5
|
+
Author-email: Jan Aerts <jan.aerts@kuleuven.be>
|
|
6
|
+
Requires-Python: >=3.11
|
|
7
|
+
Requires-Dist: anywidget[dev]>=0.9.13
|
|
8
|
+
Requires-Dist: marimo>=0.11.6
|
|
9
|
+
Requires-Dist: pandas>=2.2.3
|
|
10
|
+
Requires-Dist: requests>=2.32.3
|
|
11
|
+
Description-Content-Type: text/markdown
|
|
12
|
+
|
|
13
|
+
# Force graph widget
|
|
14
|
+
An anywidget implementation of force-graph (https://github.com/vasturiano/force-graph)
|
|
15
|
+
|
|
16
|
+
Includes a brush
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
graph_widget/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
2
|
+
graph_widget/dev.py,sha256=kekvbT2FeDGy1Tj7RMpmxOgQi0LO8lSAA7ALLgMh4KI,2630
|
|
3
|
+
graph_widget/graph.css,sha256=VstfyWUCycyslS9ArQtV1VPVVGGX95hcQ2Qm7DvG2rs,42
|
|
4
|
+
graph_widget/graph.js,sha256=A8D1Rfpd-lxX-8g43pc1kA0khZXkJikzAg69bA1JBqw,8135
|
|
5
|
+
graph_widget-0.1.0.dist-info/METADATA,sha256=qXc6Zb1dFCDFuJ-clOqxnqMHW8WQP_3w3eqBQCTX6bc,466
|
|
6
|
+
graph_widget-0.1.0.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
|
7
|
+
graph_widget-0.1.0.dist-info/RECORD,,
|