Skip to content

Commit 0cd6da5

Browse files
committed
✨ add register_click_handler
1 parent 219042b commit 0cd6da5

File tree

8 files changed

+32
-7
lines changed

8 files changed

+32
-7
lines changed

docs/documentation.adoc

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -893,12 +893,19 @@ delay(func, time, args=[], repeat=False)
893893
cancel_delay(delay_id)
894894
----
895895

896-
=== 🚧 register_click_listener
896+
=== register_click_listener
897897
[source, python]
898898
----
899899
register_click_listener(func)
900900
----
901901

902+
Registers a function that will be called when a node is clicked.
903+
904+
[cols="a,a", width="100%", options="header"]
905+
|===
906+
| Argument | Accepted Types
907+
| `func` | `Callable[Node]` (That's a function that as its only input argument takes a `Node` object that was clicked, something like `def on_click(node)`)
908+
|===
902909

903910

904911
////

pynode_next/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from .errors import *
66
from .edge import *
77
from .core import core
8-
from .graph import graph
8+
from .graph import graph, register_click_handler
99

1010
def begin_pynode_next(func, open_browser=True):
1111
if open_browser:

pynode_next/core.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ def __init__(self, port=5050):
88
self.port = 5050
99
base_path = os.path.relpath(__file__)
1010
self.custom_ui = f"{Path(base_path).parent}/ui.html"
11+
self.callback = None
1112

1213

1314
def run(self, func):
@@ -34,5 +35,6 @@ def normalise_to_canvas(self, val):
3435
"""Normalises a relative position that is an element of the interval [0, 1] to the interval [-1, 1]"""
3536
# formula used: (val - in_min) * (out_max - out_min) / (in_max - in_min) + out_min
3637
return (val * 2) - 1
38+
3739

3840
core = Core()

pynode_next/graph.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ def add_node(self, node: Node):
3939
self._nodes[node._id] = node
4040
# just sends all of the node's data
4141
core.ax(lambda x: x.dispatch(node._data()))
42+
# sets the node's click handler
43+
if core.callback != None:
44+
print("registered?", node._id)
45+
core.ax(lambda x: x.nodes([node._id]).onclick(core.callback))
46+
4247
return node
4348

4449
def remove_node(self, node):
@@ -236,6 +241,12 @@ def adjacency_matrix(self):
236241

237242
return matrix
238243

244+
def _register_click_handler(self, func):
245+
new_func = lambda n: func(self.node(n))
246+
core.callback = new_func
247+
node_list = [i._id for i in self.nodes()]
248+
core.ax(lambda x: x.nodes(node_list).onclick(new_func))
249+
239250
@staticmethod
240251
def random(order, size):
241252
"""Returns a random list of edges and nodes that may or may not be connected."""
@@ -254,3 +265,4 @@ def random(order, size):
254265

255266

256267
graph = Graph()
268+
register_click_handler = graph._register_click_handler

pynode_next/misc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@ def __str__(self):
2323
Color.BLACK = Color(0, 0, 0)
2424

2525
def pause(pause_time):
26-
core.ax(lambda x: x.pause(pause_time * 0.001))
26+
core.ax(lambda x: x.pause(pause_time * 0.001))

pynode_next/node.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,8 @@ def _data(self):
206206
"nodes": {
207207
self._id: {
208208
"color": str(self._color),
209-
"labels": {0: {"text": self._value}}
209+
"labels": {0: {"text": self._value}},
210+
"listenclick": True
210211
}
211212
}
212213
}

pynode_next/ui.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,7 @@ <h3>PNG</h3>
598598
const height = document.getElementById('output').clientHeight;
599599
// immediately resize the canvas
600600
canvas.withQ(null).duration(0).size([width, height]);
601+
601602
}
602603

603604
// main canvas
@@ -633,13 +634,14 @@ <h3>PNG</h3>
633634

634635
// open the websocket
635636
let socket = initSocket(function() {}, dispatch);
636-
637+
637638
canvas.onreceive(function(event) {
638639
// send messages to the server through the websocket
639640
socket.send(JSON.stringify(event));
640641
})
641642

642643
function start() {
644+
643645
if (running === false) {
644646
// trigger start
645647
canvas.message('start');

test.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
import random
55

66
def test():
7-
graph.add_node("(0, 0)").set_position(0, 0)
8-
graph.add_node("(1, 1)").set_position(1, 1)
7+
graph.add_node("a")
8+
register_click_handler(lambda x: print(x._data()))
9+
910

1011
begin_pynode_next(test)

0 commit comments

Comments
 (0)