graph_widget 0.1.16__tar.gz → 0.1.17__tar.gz

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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: graph_widget
3
- Version: 0.1.16
3
+ Version: 0.1.17
4
4
  Requires-Dist: anywidget
5
5
  Requires-Dist: marimo
6
6
  Description-Content-Type: text/markdown
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
4
4
 
5
5
  [project]
6
6
  name = "graph_widget"
7
- version = "0.1.16"
7
+ version = "0.1.17"
8
8
  dependencies = ["anywidget","marimo"]
9
9
  readme = "README.md"
10
10
 
@@ -0,0 +1,91 @@
1
+ import marimo
2
+
3
+ __generated_with = "0.11.6"
4
+ app = marimo.App(width="full")
5
+
6
+
7
+ @app.cell
8
+ def _():
9
+ import graph_widget
10
+ import marimo as mo
11
+ return graph_widget, mo
12
+
13
+
14
+ @app.cell
15
+ def _():
16
+ # 34 breeds to simulate the "too many colours" problem
17
+ breeds = [
18
+ "ARG", "BEL", "BRA", "CAN", "CHE", "CHN", "COL", "CZE", "DEU", "DNK",
19
+ "ESP", "FIN", "FRA", "GBR", "GRC", "HUN", "IND", "IRL", "ISR", "ITA",
20
+ "JPN", "KOR", "MEX", "NLD", "NOR", "NZL", "POL", "PRT", "ROU", "RUS",
21
+ "SWE", "TUR", "USA", "ZAF",
22
+ ]
23
+
24
+ nodes = []
25
+ node_id = 0
26
+ for breed in breeds:
27
+ for i in range(3):
28
+ nodes.append({"id": node_id, "breed": breed, "animal": f"{breed}{i+1}"})
29
+ node_id += 1
30
+
31
+ links = []
32
+ for i in range(0, len(nodes) - 1, 3):
33
+ links.append({"source": nodes[i]["id"], "target": nodes[i+1]["id"]})
34
+ links.append({"source": nodes[i+1]["id"], "target": nodes[i+2]["id"]})
35
+ if i + 3 < len(nodes):
36
+ links.append({"source": nodes[i]["id"], "target": nodes[i+3]["id"]})
37
+
38
+ data = {"nodes": nodes, "links": links}
39
+ return breeds, data, links, nodes
40
+
41
+
42
+ @app.cell
43
+ def _(breeds, mo):
44
+ select_feature_dropdown = mo.ui.dropdown(
45
+ options=["breed"],
46
+ value="breed",
47
+ label="Select feature",
48
+ )
49
+ select_value_dropdown = mo.ui.dropdown(
50
+ options=[""] + breeds,
51
+ value="",
52
+ label="Highlight breed",
53
+ )
54
+ colour_feature_dropdown = mo.ui.dropdown(
55
+ options=["breed"],
56
+ value="breed",
57
+ label="Colour by",
58
+ )
59
+ return colour_feature_dropdown, select_feature_dropdown, select_value_dropdown
60
+
61
+
62
+ @app.cell
63
+ def _(colour_feature_dropdown, data, graph_widget, mo, select_feature_dropdown, select_value_dropdown):
64
+ data_graph = mo.ui.anywidget(
65
+ graph_widget.ForceGraphWidget(
66
+ data=data,
67
+ repulsion=50,
68
+ node_scale=3,
69
+ colour_feature=colour_feature_dropdown.value,
70
+ select_feature=select_feature_dropdown.value,
71
+ select_feature_value=select_value_dropdown.value,
72
+ )
73
+ )
74
+ return (data_graph,)
75
+
76
+
77
+ @app.cell
78
+ def _(colour_feature_dropdown, data_graph, mo, select_feature_dropdown, select_value_dropdown):
79
+ mo.hstack([
80
+ data_graph,
81
+ mo.vstack([
82
+ colour_feature_dropdown,
83
+ select_feature_dropdown,
84
+ select_value_dropdown,
85
+ mo.md(f"**Selected IDs:** {data_graph.widget.selected_ids}"),
86
+ ]),
87
+ ], justify="start")
88
+
89
+
90
+ if __name__ == "__main__":
91
+ app.run()
@@ -21,5 +21,16 @@ class ForceGraphWidget(anywidget.AnyWidget):
21
21
  colour_feature = traitlets.Unicode().tag(sync=True)
22
22
  colour_scale_type = traitlets.Unicode().tag(sync=True)
23
23
  selected_ids = traitlets.List([]).tag(sync=True)
24
+ select_feature = traitlets.Unicode().tag(sync=True)
25
+ select_feature_value = traitlets.Unicode().tag(sync=True)
24
26
  width = traitlets.Int().tag(sync=True)
25
- height = traitlets.Int().tag(sync=True)
27
+ height = traitlets.Int().tag(sync=True)
28
+
29
+ def __init__(self, *args, **kwargs):
30
+ super().__init__(*args, **kwargs)
31
+ if self.select_feature and self.select_feature_value:
32
+ self.selected_ids = [
33
+ node["id"]
34
+ for node in self.data.get("nodes", [])
35
+ if str(node.get(self.select_feature, "")) == str(self.select_feature_value)
36
+ ]
@@ -26,6 +26,8 @@ let default_node_size = 5;
26
26
  let colour_feature = undefined;
27
27
  let node_size_feature = undefined;
28
28
  let colour_scale;
29
+ let select_feature = undefined;
30
+ let select_feature_value = undefined;
29
31
 
30
32
  function debounce(func, wait) {
31
33
  let timeout;
@@ -63,8 +65,10 @@ const create_node_canvas_object = (plot, node_scale, node_size_feature) => {
63
65
  radius = node[node_size_feature] * node_scale
64
66
  }
65
67
  const isLocalSelected = local_selected_ids.includes(node.id);
68
+ const hasSelection = local_selected_ids.length > 0;
66
69
  const fillColour = colour_scale(node[colour_feature]);
67
- // const radius = node[node_size_feature] * node_scale;
70
+
71
+ ctx.globalAlpha = hasSelection && !isLocalSelected ? 0.2 : 1.0;
68
72
 
69
73
  // Draw filled circle
70
74
  ctx.beginPath();
@@ -82,6 +86,8 @@ const create_node_canvas_object = (plot, node_scale, node_size_feature) => {
82
86
  ctx.strokeStyle = 'lightgrey';
83
87
  ctx.stroke();
84
88
  }
89
+
90
+ ctx.globalAlpha = 1.0;
85
91
  });
86
92
  };
87
93
 
@@ -114,6 +120,8 @@ function render({ model, el }) {
114
120
  colour_scale_type = model.get("colour_scale_type");
115
121
  colour_feature = model.get("colour_feature");
116
122
  node_size_feature = model.get("node_size_feature");
123
+ select_feature = model.get("select_feature");
124
+ select_feature_value = model.get("select_feature_value");
117
125
  let global_selected_ids = model.get("selected_ids");
118
126
 
119
127
  plot = create_plot(data);
@@ -201,6 +209,23 @@ function render({ model, el }) {
201
209
  }
202
210
  model.on("change:colour_scale_type", update_colour_scale_type)
203
211
 
212
+ const apply_select_feature = () => {
213
+ select_feature = model.get("select_feature");
214
+ select_feature_value = model.get("select_feature_value");
215
+ if (select_feature && select_feature_value !== undefined && select_feature_value !== "") {
216
+ local_selected_ids = data.nodes
217
+ .filter(node => String(node[select_feature]) === String(select_feature_value))
218
+ .map(node => node.id);
219
+ } else {
220
+ local_selected_ids = [];
221
+ }
222
+ model.set("selected_ids", local_selected_ids);
223
+ debouncedSaveChanges();
224
+ create_node_canvas_object(plot, node_scale, node_size_feature);
225
+ }
226
+ model.on("change:select_feature", apply_select_feature);
227
+ model.on("change:select_feature_value", apply_select_feature);
228
+
204
229
  model.on("change:selected_ids", () => {
205
230
  global_selected_ids = model.get("selected_ids");
206
231
  plot.nodeColor((d) =>
@@ -215,6 +240,7 @@ function render({ model, el }) {
215
240
  update_node_scale();
216
241
  update_colour_feature();
217
242
  update_node_size_feature();
243
+ apply_select_feature();
218
244
 
219
245
  let brush_active = false;
220
246
 
@@ -100,7 +100,7 @@ wheels = [
100
100
 
101
101
  [[package]]
102
102
  name = "graph-widget"
103
- version = "0.1.15"
103
+ version = "0.1.16"
104
104
  source = { editable = "." }
105
105
  dependencies = [
106
106
  { name = "anywidget" },
File without changes
File without changes
File without changes
File without changes
File without changes