File size: 2,153 Bytes
e873747
04ce1c2
 
 
 
 
 
 
 
 
 
 
e873747
 
 
04ce1c2
 
 
e873747
04ce1c2
 
 
 
 
 
 
 
e873747
04ce1c2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import panel as pn
import param
from panel.reactive import ReactiveHTML

layouts = {
    "cose": {"name": "cose"},
    "grid": {"name": "grid"},
    "circle": {"name": "circle"},
    "concentric": {"name": "concentric"},
    "breadthfirst": {"name": "breadthfirst"},
    "random": {"name": "random"},
    "preset": {"name": "preset"},
}


class Cytoscape(ReactiveHTML):
    layout_names = param.ObjectSelector(
        default="cose", doc="Layout Options to choose from.", objects=list(layouts.keys())
    )
    graph_layouts = param.Dict(default=layouts)
    style = param.List(doc="Use to set the styles of the nodes/edges")
    data = param.List(doc="Use to send node's data/attributes to Cytoscape")

    _template = """
        <div id="pnx-container">
            <div id="cy" style="position: relative; width: 200px; height: 200px; border: 1px solid"></div>
        </div>
    """
    __javascript__ = ["https://cdnjs.cloudflare.com/ajax/libs/cytoscape/3.25.0/cytoscape.umd.js"]

    _scripts = {
        "after_layout": """
        self.create()
        """,
        "create": """
            state.cy = cytoscape({
              container: cy,
              layout: {name: 'cose'}, 
              elements: [{data:{id:'A', label:'A'}},{data:{id:'B', label:'B'}}, {data:{source:'A', target:'B'}}],
              zoom: 1,
              pan: {x: 0, y: 0},
            });
            state.cy.on('click', function (evt) {
                console.log(evt.originalEvent, evt)
                state.cy.resize();
            });
        """,
        "remove": """
        state.cy.destroy()
        delete state.cy
        """,
        "layout_names": """
            console.log("layout_names")
            const layout = state.cy.layout(data.graph_layouts[data.layout_names])
            layout.run()
        """,
    }

    _extension_name = "cytoscape"


pn.extension("cytoscape", template="fast")

my_graph = Cytoscape(width=800, height=400)

widget_container = pn.Column(pn.pane.Markdown("## Title"))
app = pn.Column(widget_container).servable(target="main")
widget_container.append(pn.Row(my_graph.param.layout_names, my_graph))

app.servable()