pyironflow 0.0.1__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.
- pyironflow-0.0.1/LICENSE +29 -0
- pyironflow-0.0.1/MANIFEST.in +7 -0
- pyironflow-0.0.1/PKG-INFO +98 -0
- pyironflow-0.0.1/README.md +39 -0
- pyironflow-0.0.1/js/CustomNode.jsx +210 -0
- pyironflow-0.0.1/js/TextUpdaterNode.jsx +43 -0
- pyironflow-0.0.1/js/text-updater-node.css +29 -0
- pyironflow-0.0.1/js/widget-drag-drop.jsx +154 -0
- pyironflow-0.0.1/js/widget.css +12 -0
- pyironflow-0.0.1/js/widget.jsx +201 -0
- pyironflow-0.0.1/pyironflow/__init__.py +5 -0
- pyironflow-0.0.1/pyironflow/_version.py +21 -0
- pyironflow-0.0.1/pyironflow/pyironflow.py +67 -0
- pyironflow-0.0.1/pyironflow/reactflow.py +295 -0
- pyironflow-0.0.1/pyironflow/treeview.py +232 -0
- pyironflow-0.0.1/pyironflow.egg-info/PKG-INFO +98 -0
- pyironflow-0.0.1/pyironflow.egg-info/SOURCES.txt +21 -0
- pyironflow-0.0.1/pyironflow.egg-info/dependency_links.txt +1 -0
- pyironflow-0.0.1/pyironflow.egg-info/requires.txt +9 -0
- pyironflow-0.0.1/pyironflow.egg-info/top_level.txt +1 -0
- pyironflow-0.0.1/pyproject.toml +70 -0
- pyironflow-0.0.1/setup.cfg +4 -0
- pyironflow-0.0.1/setup.py +8 -0
pyironflow-0.0.1/LICENSE
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
BSD 3-Clause License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2018, Max-Planck-Institut für Eisenforschung GmbH - Computational Materials Design (CM) Department
|
|
4
|
+
All rights reserved.
|
|
5
|
+
|
|
6
|
+
Redistribution and use in source and binary forms, with or without
|
|
7
|
+
modification, are permitted provided that the following conditions are met:
|
|
8
|
+
|
|
9
|
+
* Redistributions of source code must retain the above copyright notice, this
|
|
10
|
+
list of conditions and the following disclaimer.
|
|
11
|
+
|
|
12
|
+
* Redistributions in binary form must reproduce the above copyright notice,
|
|
13
|
+
this list of conditions and the following disclaimer in the documentation
|
|
14
|
+
and/or other materials provided with the distribution.
|
|
15
|
+
|
|
16
|
+
* Neither the name of the copyright holder nor the names of its
|
|
17
|
+
contributors may be used to endorse or promote products derived from
|
|
18
|
+
this software without specific prior written permission.
|
|
19
|
+
|
|
20
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
21
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
22
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
23
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
24
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
25
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
26
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
27
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
28
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
29
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: pyironflow
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: react xyflow for pyiron
|
|
5
|
+
Author-email: "Max-Planck-Institut für Eisenforschung GmbH - Computational Materials Design (CM) Department" <pyiron@mpie.de>
|
|
6
|
+
License: BSD 3-Clause License
|
|
7
|
+
|
|
8
|
+
Copyright (c) 2018, Max-Planck-Institut für Eisenforschung GmbH - Computational Materials Design (CM) Department
|
|
9
|
+
All rights reserved.
|
|
10
|
+
|
|
11
|
+
Redistribution and use in source and binary forms, with or without
|
|
12
|
+
modification, are permitted provided that the following conditions are met:
|
|
13
|
+
|
|
14
|
+
* Redistributions of source code must retain the above copyright notice, this
|
|
15
|
+
list of conditions and the following disclaimer.
|
|
16
|
+
|
|
17
|
+
* Redistributions in binary form must reproduce the above copyright notice,
|
|
18
|
+
this list of conditions and the following disclaimer in the documentation
|
|
19
|
+
and/or other materials provided with the distribution.
|
|
20
|
+
|
|
21
|
+
* Neither the name of the copyright holder nor the names of its
|
|
22
|
+
contributors may be used to endorse or promote products derived from
|
|
23
|
+
this software without specific prior written permission.
|
|
24
|
+
|
|
25
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
26
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
27
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
28
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
29
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
30
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
31
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
32
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
33
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
34
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
35
|
+
|
|
36
|
+
Project-URL: Homepage, https://github.com/pyiron/pyironFlow
|
|
37
|
+
Project-URL: Documentation, https://github.com/pyiron/pyironFlow
|
|
38
|
+
Project-URL: Repository, https://github.com/pyiron/pyironFlow
|
|
39
|
+
Keywords: pyiron
|
|
40
|
+
Classifier: Development Status :: 2 - Pre-Alpha
|
|
41
|
+
Classifier: Topic :: Scientific/Engineering :: Physics
|
|
42
|
+
Classifier: License :: OSI Approved :: BSD License
|
|
43
|
+
Classifier: Intended Audience :: Science/Research
|
|
44
|
+
Classifier: Operating System :: OS Independent
|
|
45
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
46
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
47
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
48
|
+
Requires-Python: <3.13,>=3.9
|
|
49
|
+
Description-Content-Type: text/markdown
|
|
50
|
+
License-File: LICENSE
|
|
51
|
+
Requires-Dist: anywidget
|
|
52
|
+
Requires-Dist: traitlets
|
|
53
|
+
Requires-Dist: ipython
|
|
54
|
+
Requires-Dist: ipywidgets
|
|
55
|
+
Requires-Dist: pyiron_workflow
|
|
56
|
+
Provides-Extra: dev
|
|
57
|
+
Requires-Dist: watchfiles; extra == "dev"
|
|
58
|
+
Requires-Dist: jupyterlab; extra == "dev"
|
|
59
|
+
|
|
60
|
+
# pyironFlow
|
|
61
|
+
|
|
62
|
+
# Visual Programming Interface
|
|
63
|
+
The visual programmming interface `pyironflow` or `PyironFlow` is a gui skin based on [ReactFlow](https://reactflow.dev/) that currently works on top of `pyiron_workflow`. Theoretically, one could currently pack `pyiron_base` jobs into nodes for execution. The gui could also be extended to pack the workflow graph (extracted from the gui using `get_workflow()`) into a `pyiron_base` job for execution. An existing code-based workflow graph can be packed into the gui using `PyironFlow([wf])` where wf is the existing graph.
|
|
64
|
+
|
|
65
|
+
## Example of a multiscale simulation
|
|
66
|
+
|
|
67
|
+
### Problem definition:
|
|
68
|
+

|
|
69
|
+
|
|
70
|
+
### Constructing the workflow:
|
|
71
|
+

|
|
72
|
+
|
|
73
|
+
### Execution:
|
|
74
|
+

|
|
75
|
+
|
|
76
|
+
### Viewing source code:
|
|
77
|
+

|
|
78
|
+
|
|
79
|
+
### Visualization of FEM meshes using PyVista:
|
|
80
|
+

|
|
81
|
+
|
|
82
|
+
### Atomistic sub-workflow:
|
|
83
|
+

|
|
84
|
+
|
|
85
|
+
### Continuum sub-workflow:
|
|
86
|
+

|
|
87
|
+
|
|
88
|
+
## Planned/desired/in-development features
|
|
89
|
+
|
|
90
|
+
- Dynamically add nodes from the notebook to the node library (currently being polished by Julius Mittler)
|
|
91
|
+
- Dynamically make macros from the gui using a box selection and add them to the node library (currently in development by Julius Mittler)
|
|
92
|
+
- Automatic positioning of nodes in a nice way on (re-)instantiation (current positioning is passable, but room for improvement exists)
|
|
93
|
+
- Caching based on a hash-based storage. Currently caching does not work at all in the gui since the workflow graph is reinstantiated everytime there's a change in the gui. This could be theoretically fixed by comparing the dictionary of the existing workflow to the new dictionary from the gui. But this would be a painstaking process and easily broken. A better way would be to work with a hash-based storage which would reload outputs when the inputs of a node have not changed. However this would have to be implemented in `pyiron_workflow` and not in the gui side of things.
|
|
94
|
+
- Edit the source code of nodes from within the gui. Maybe something that uses the [FileReader](https://stackoverflow.com/questions/51272255/how-to-use-filereader-in-react) api. Need to look into this further.
|
|
95
|
+
|
|
96
|
+
See the demo.ipynb jupyter notebook for brief discussion of the key ideas, the link to pyiron_workflows and a few toy application of the xyflow project.
|
|
97
|
+
|
|
98
|
+
[](https://mybinder.org/v2/gh/pyiron/pyironFlow/HEAD?labpath=pyironflow_demo.ipynb)
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# pyironFlow
|
|
2
|
+
|
|
3
|
+
# Visual Programming Interface
|
|
4
|
+
The visual programmming interface `pyironflow` or `PyironFlow` is a gui skin based on [ReactFlow](https://reactflow.dev/) that currently works on top of `pyiron_workflow`. Theoretically, one could currently pack `pyiron_base` jobs into nodes for execution. The gui could also be extended to pack the workflow graph (extracted from the gui using `get_workflow()`) into a `pyiron_base` job for execution. An existing code-based workflow graph can be packed into the gui using `PyironFlow([wf])` where wf is the existing graph.
|
|
5
|
+
|
|
6
|
+
## Example of a multiscale simulation
|
|
7
|
+
|
|
8
|
+
### Problem definition:
|
|
9
|
+

|
|
10
|
+
|
|
11
|
+
### Constructing the workflow:
|
|
12
|
+

|
|
13
|
+
|
|
14
|
+
### Execution:
|
|
15
|
+

|
|
16
|
+
|
|
17
|
+
### Viewing source code:
|
|
18
|
+

|
|
19
|
+
|
|
20
|
+
### Visualization of FEM meshes using PyVista:
|
|
21
|
+

|
|
22
|
+
|
|
23
|
+
### Atomistic sub-workflow:
|
|
24
|
+

|
|
25
|
+
|
|
26
|
+
### Continuum sub-workflow:
|
|
27
|
+

|
|
28
|
+
|
|
29
|
+
## Planned/desired/in-development features
|
|
30
|
+
|
|
31
|
+
- Dynamically add nodes from the notebook to the node library (currently being polished by Julius Mittler)
|
|
32
|
+
- Dynamically make macros from the gui using a box selection and add them to the node library (currently in development by Julius Mittler)
|
|
33
|
+
- Automatic positioning of nodes in a nice way on (re-)instantiation (current positioning is passable, but room for improvement exists)
|
|
34
|
+
- Caching based on a hash-based storage. Currently caching does not work at all in the gui since the workflow graph is reinstantiated everytime there's a change in the gui. This could be theoretically fixed by comparing the dictionary of the existing workflow to the new dictionary from the gui. But this would be a painstaking process and easily broken. A better way would be to work with a hash-based storage which would reload outputs when the inputs of a node have not changed. However this would have to be implemented in `pyiron_workflow` and not in the gui side of things.
|
|
35
|
+
- Edit the source code of nodes from within the gui. Maybe something that uses the [FileReader](https://stackoverflow.com/questions/51272255/how-to-use-filereader-in-react) api. Need to look into this further.
|
|
36
|
+
|
|
37
|
+
See the demo.ipynb jupyter notebook for brief discussion of the key ideas, the link to pyiron_workflows and a few toy application of the xyflow project.
|
|
38
|
+
|
|
39
|
+
[](https://mybinder.org/v2/gh/pyiron/pyironFlow/HEAD?labpath=pyironflow_demo.ipynb)
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
import React, { memo, useEffect, useState } from "react";
|
|
2
|
+
import { Handle, useUpdateNodeInternals, NodeToolbar, useNodesState, } from "@xyflow/react";
|
|
3
|
+
import { useModel } from "@anywidget/react";
|
|
4
|
+
import { UpdateDataContext } from './widget.jsx'; // import the context
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Author: Joerg Neugebauer
|
|
8
|
+
* Copyright: Copyright 2024, Max-Planck-Institut for Sustainable Materials GmbH - Computational Materials Design (CM) Department
|
|
9
|
+
* Version: 0.2
|
|
10
|
+
* Maintainer:
|
|
11
|
+
* Email:
|
|
12
|
+
* Status: development
|
|
13
|
+
* Date: Aug 1, 2024
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
export default memo(({ data }) => {
|
|
17
|
+
const updateNodeInternals = useUpdateNodeInternals();
|
|
18
|
+
// const [nodes, setNodes, onNodesChange] = useNodesState([]);
|
|
19
|
+
|
|
20
|
+
const num_handles = Math.max(data.source_labels.length, data.target_labels.length);
|
|
21
|
+
const [handles, setHandles] = useState(Array(num_handles).fill({}));
|
|
22
|
+
|
|
23
|
+
const model = useModel();
|
|
24
|
+
const context = React.useContext(UpdateDataContext);
|
|
25
|
+
|
|
26
|
+
// console.log('nodes', nodes)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
useEffect(() => {
|
|
30
|
+
handles.map((_, index) => {
|
|
31
|
+
updateNodeInternals(`handle-${index}`);
|
|
32
|
+
});
|
|
33
|
+
}, [handles]);
|
|
34
|
+
|
|
35
|
+
const runFunction = () => {
|
|
36
|
+
// run the node
|
|
37
|
+
console.log('run: ', data.label)
|
|
38
|
+
model.set("commands", `run: ${data.label} - ${new Date().getTime()}`);
|
|
39
|
+
model.save_changes();
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const outputFunction = () => {
|
|
43
|
+
// direct output of node to output widget
|
|
44
|
+
console.log('output: ', data.label)
|
|
45
|
+
model.set("commands", `output: ${data.label}`);
|
|
46
|
+
model.save_changes();
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const sourceFunction = () => {
|
|
50
|
+
// show source code of node
|
|
51
|
+
console.log('source: ', data.label)
|
|
52
|
+
model.set("commands", `source: ${data.label}`);
|
|
53
|
+
model.save_changes();
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
const renderLabel = (label) => {
|
|
58
|
+
return (
|
|
59
|
+
<div style={{ fontWeight: "normal", marginBottom: "0.3em", textAlign: "center" }}>
|
|
60
|
+
{label}
|
|
61
|
+
</div>
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
const renderCustomHandle = (position, type, index, label) => {
|
|
67
|
+
return (
|
|
68
|
+
<Handle
|
|
69
|
+
key={`${position}-${index}`}
|
|
70
|
+
type={type}
|
|
71
|
+
position={position}
|
|
72
|
+
id={label}
|
|
73
|
+
style={{ top: 30 + 16 * index}}
|
|
74
|
+
/>
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const renderInputHandle = (data, index, editValue = false) => {
|
|
79
|
+
const label = data.target_labels[index]
|
|
80
|
+
const inp_type = data.target_types[index]
|
|
81
|
+
const value = data.target_values[index]
|
|
82
|
+
const [inputValue, setInputValue] = useState(value);
|
|
83
|
+
const context = React.useContext(UpdateDataContext);
|
|
84
|
+
// console.log('input type: ', data)
|
|
85
|
+
|
|
86
|
+
const inputTypeMap = {
|
|
87
|
+
'str': 'text',
|
|
88
|
+
'int': 'text',
|
|
89
|
+
'float': 'text',
|
|
90
|
+
'bool': 'checkbox'
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
const convertInput = (value, inp_type) => {
|
|
94
|
+
switch(inp_type) {
|
|
95
|
+
case 'int':
|
|
96
|
+
// Check if value can be converted to an integer
|
|
97
|
+
const intValue = parseInt(value, 10);
|
|
98
|
+
return isNaN(intValue) ? value : intValue;
|
|
99
|
+
case 'float':
|
|
100
|
+
// Check if value can be converted to a float
|
|
101
|
+
const floatValue = parseFloat(value);
|
|
102
|
+
return isNaN(floatValue) ? value : floatValue;
|
|
103
|
+
case 'bool':
|
|
104
|
+
return value;
|
|
105
|
+
default:
|
|
106
|
+
return value; // if inp_type === 'str' or anything else unexpected, returns the original string
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const currentInputType = inputTypeMap[inp_type] || 'text';
|
|
111
|
+
|
|
112
|
+
if (inp_type === 'NonPrimitive' || inp_type === 'None') {
|
|
113
|
+
editValue = false;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const getBackgroundColor = (value, inp_type) => {
|
|
117
|
+
if (value === null) {
|
|
118
|
+
return 'grey';
|
|
119
|
+
} else if (value === 'NotData') {
|
|
120
|
+
return 'red'; // please remember to use a proper CSS color or RGB code
|
|
121
|
+
} else {
|
|
122
|
+
return 'white';
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
return (
|
|
127
|
+
<>
|
|
128
|
+
<div style={{ height: 16, fontSize: '10px', display: 'flex', alignItems: 'center', flexDirection: 'row-reverse', justifyContent: 'flex-end' }}>
|
|
129
|
+
<span style={{ marginLeft: '5px' }}>{`${label}`}</span>
|
|
130
|
+
{editValue
|
|
131
|
+
? <input
|
|
132
|
+
type={currentInputType}
|
|
133
|
+
checked={currentInputType === 'checkbox' ? inputValue : undefined}
|
|
134
|
+
value={currentInputType !== 'checkbox' ? inputValue : undefined}
|
|
135
|
+
className="nodrag"
|
|
136
|
+
onChange={e => {
|
|
137
|
+
const newValue = currentInputType === 'checkbox' ? e.target.checked : e.target.value;
|
|
138
|
+
console.log('onChange', value, e, inputValue, newValue, index, data.label);
|
|
139
|
+
// Always update the input value
|
|
140
|
+
setInputValue(newValue);
|
|
141
|
+
context(data.label, index, newValue);
|
|
142
|
+
}}
|
|
143
|
+
onKeyDown={e => {
|
|
144
|
+
if(e.keyCode === 13) {
|
|
145
|
+
// When Enter key is pressed, convert the input
|
|
146
|
+
const convertedValue = convertInput(inputValue, inp_type);
|
|
147
|
+
console.log('onKeyDown', value, e, inputValue, convertedValue, index, data.label);
|
|
148
|
+
context(data.label, index, convertedValue);
|
|
149
|
+
}
|
|
150
|
+
}}
|
|
151
|
+
onBlur={() => {
|
|
152
|
+
// When the mouse leaves the textbox, convert the input
|
|
153
|
+
const convertedValue = convertInput(inputValue, inp_type);
|
|
154
|
+
context(data.label, index, convertedValue);
|
|
155
|
+
}}
|
|
156
|
+
style={{
|
|
157
|
+
width: '15px',
|
|
158
|
+
height: '10px',
|
|
159
|
+
fontSize: '6px',
|
|
160
|
+
backgroundColor: getBackgroundColor(value, inp_type)
|
|
161
|
+
}}
|
|
162
|
+
/>
|
|
163
|
+
: ''
|
|
164
|
+
}
|
|
165
|
+
</div>
|
|
166
|
+
{renderCustomHandle('left', 'target', index, label)}
|
|
167
|
+
</>
|
|
168
|
+
);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
const renderOutputHandle = (data, index) => {
|
|
172
|
+
const label = data.source_labels[index]
|
|
173
|
+
|
|
174
|
+
return (
|
|
175
|
+
<>
|
|
176
|
+
<div style={{ height: 16, fontSize: '10px', textAlign: 'right' }}>
|
|
177
|
+
{`${label}`}
|
|
178
|
+
</div>
|
|
179
|
+
{renderCustomHandle('right', 'source', index, label)}
|
|
180
|
+
</>
|
|
181
|
+
);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
return (
|
|
186
|
+
<div>
|
|
187
|
+
|
|
188
|
+
{renderLabel(data.label)}
|
|
189
|
+
|
|
190
|
+
<div>
|
|
191
|
+
{handles.map((_, index) => (
|
|
192
|
+
<div style={{ display: "flex", justifyContent: "space-between", alignItems: "center" }}>
|
|
193
|
+
{index < data.target_labels.length &&
|
|
194
|
+
renderInputHandle(data, index, true)}
|
|
195
|
+
|
|
196
|
+
{index < data.source_labels.length &&
|
|
197
|
+
renderOutputHandle(data, index)}
|
|
198
|
+
</div>
|
|
199
|
+
))}
|
|
200
|
+
</div>
|
|
201
|
+
<NodeToolbar
|
|
202
|
+
isVisible={data.forceToolbarVisible || undefined}
|
|
203
|
+
position={data.toolbarPosition}
|
|
204
|
+
>
|
|
205
|
+
<button onClick={runFunction}>Run</button>
|
|
206
|
+
<button onClick={sourceFunction}>Source</button>
|
|
207
|
+
</NodeToolbar>
|
|
208
|
+
</div>
|
|
209
|
+
);
|
|
210
|
+
});
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import React, { useCallback } from 'react';
|
|
2
|
+
import { Handle, Position } from '@xyflow/react';
|
|
3
|
+
|
|
4
|
+
const handleStyle = { right: 0, top: 10 }; //, width: 12, height: 12 };
|
|
5
|
+
|
|
6
|
+
function TextUpdaterNode({ data, isConnectable }) {
|
|
7
|
+
const onChange = useCallback((evt) => {
|
|
8
|
+
console.log(evt.target.value);
|
|
9
|
+
}, []);
|
|
10
|
+
|
|
11
|
+
return (
|
|
12
|
+
<div className="text-updater-node">
|
|
13
|
+
<Handle
|
|
14
|
+
type="target"
|
|
15
|
+
position={Position.Left}
|
|
16
|
+
isConnectable={isConnectable}>
|
|
17
|
+
<div className="handle-text-left">target</div>
|
|
18
|
+
</Handle>
|
|
19
|
+
<div>
|
|
20
|
+
<label htmlFor="text">Text:</label>
|
|
21
|
+
<input id="text" name="text" onChange={onChange} className="nodrag" />
|
|
22
|
+
</div>
|
|
23
|
+
<Handle
|
|
24
|
+
type="source"
|
|
25
|
+
position={Position.Right}
|
|
26
|
+
id="a"
|
|
27
|
+
style={handleStyle}
|
|
28
|
+
isConnectable={isConnectable}>
|
|
29
|
+
<div className="handle-text-right">source a</div>
|
|
30
|
+
</Handle>
|
|
31
|
+
|
|
32
|
+
<Handle
|
|
33
|
+
type="source"
|
|
34
|
+
position={Position.Right}
|
|
35
|
+
id="b"
|
|
36
|
+
isConnectable={isConnectable}>
|
|
37
|
+
<div className="handle-text-right">source b</div>
|
|
38
|
+
</Handle>
|
|
39
|
+
</div>
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export default TextUpdaterNode;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
.text-updater-node {
|
|
2
|
+
height: 50px;
|
|
3
|
+
border: 1px solid #eee;
|
|
4
|
+
padding: 5px;
|
|
5
|
+
border-radius: 5px;
|
|
6
|
+
background: white;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
.text-updater-node label {
|
|
10
|
+
display: block;
|
|
11
|
+
color: #777;
|
|
12
|
+
font-size: 12px;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
.handle-text-left {
|
|
16
|
+
position: relative;
|
|
17
|
+
width: 100px;
|
|
18
|
+
transform: translate(10px, 0%);
|
|
19
|
+
font-size: 6px;
|
|
20
|
+
pointer-events: none;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
.handle-text-right {
|
|
24
|
+
position: absolute;
|
|
25
|
+
right: 10px; /* this is the constant value for the right-side spacing, set it as per your requirements */
|
|
26
|
+
width: 30px;
|
|
27
|
+
font-size: 6px;
|
|
28
|
+
pointer-events: none;
|
|
29
|
+
}
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
// does not work (Error: [React Flow]: Seems like you have not used zustand provider as an ancestor.)
|
|
2
|
+
// It is likely a limitation of anywidget (only a single react tree)
|
|
3
|
+
// ipyreact should be able to overcome it
|
|
4
|
+
|
|
5
|
+
import React, { useCallback, useState } from 'react';
|
|
6
|
+
import { createRender, useModel } from "@anywidget/react";
|
|
7
|
+
import {
|
|
8
|
+
ReactFlow,
|
|
9
|
+
Controls,
|
|
10
|
+
MiniMap,
|
|
11
|
+
Background,
|
|
12
|
+
useNodesState,
|
|
13
|
+
useEdgesState,
|
|
14
|
+
useReactFlow,
|
|
15
|
+
applyEdgeChanges,
|
|
16
|
+
applyNodeChanges,
|
|
17
|
+
addEdge,} from '@xyflow/react';
|
|
18
|
+
import '@xyflow/react/dist/style.css';
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
import TextUpdaterNode from './TextUpdaterNode.jsx';
|
|
22
|
+
import CustomNode from './CustomNode.jsx';
|
|
23
|
+
|
|
24
|
+
import './text-updater-node.css';
|
|
25
|
+
|
|
26
|
+
const rfStyle = {
|
|
27
|
+
backgroundColor: '#B8CEFF',
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
const nodeTypes = { textUpdater: TextUpdaterNode, customNode: CustomNode };
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
const render = createRender(() => {
|
|
35
|
+
// function render({ model, el }) {
|
|
36
|
+
const model = useModel();
|
|
37
|
+
// console.log("model: ", model);
|
|
38
|
+
const initialNodes = JSON.parse(model.get("nodes"))
|
|
39
|
+
const initialEdges = JSON.parse(model.get("edges"))
|
|
40
|
+
|
|
41
|
+
const [nodes, setNodes] = useState(initialNodes);
|
|
42
|
+
const [edges, setEdges] = useState(initialEdges);
|
|
43
|
+
const { screenToFlowPosition } = useReactFlow();
|
|
44
|
+
|
|
45
|
+
model.on("change:nodes", () => {
|
|
46
|
+
const new_nodes = model.get("nodes")
|
|
47
|
+
// console.log("load nodes: ", new_nodes);
|
|
48
|
+
|
|
49
|
+
setNodes(JSON.parse(new_nodes));
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
model.on("change:edges", () => {
|
|
53
|
+
const new_edges = model.get("edges")
|
|
54
|
+
setEdges(JSON.parse(new_edges));
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
const onNodesChange = useCallback(
|
|
58
|
+
(changes) => {
|
|
59
|
+
setNodes((nds) => {
|
|
60
|
+
const new_nodes = applyNodeChanges(changes, nds);
|
|
61
|
+
model.set("nodes", JSON.stringify(new_nodes));
|
|
62
|
+
model.save_changes();
|
|
63
|
+
return new_nodes;
|
|
64
|
+
});
|
|
65
|
+
},
|
|
66
|
+
[setNodes],
|
|
67
|
+
);
|
|
68
|
+
|
|
69
|
+
const onEdgesChange = useCallback(
|
|
70
|
+
(changes) => {
|
|
71
|
+
setEdges((eds) => {
|
|
72
|
+
const new_edges = applyEdgeChanges(changes, eds);
|
|
73
|
+
model.set("edges", JSON.stringify(new_edges));
|
|
74
|
+
model.save_changes();
|
|
75
|
+
return new_edges;
|
|
76
|
+
});
|
|
77
|
+
},
|
|
78
|
+
[setEdges],
|
|
79
|
+
);
|
|
80
|
+
|
|
81
|
+
const onConnect = useCallback(
|
|
82
|
+
(params) => {
|
|
83
|
+
setEdges((eds) => {
|
|
84
|
+
const new_edges = addEdge(params, eds);
|
|
85
|
+
model.set("edges", JSON.stringify(new_edges));
|
|
86
|
+
model.save_changes();
|
|
87
|
+
return new_edges;
|
|
88
|
+
});
|
|
89
|
+
},
|
|
90
|
+
[setEdges],
|
|
91
|
+
);
|
|
92
|
+
|
|
93
|
+
const onDragOver = useCallback((event) => {
|
|
94
|
+
event.preventDefault();
|
|
95
|
+
event.dataTransfer.dropEffect = 'move';
|
|
96
|
+
}, []);
|
|
97
|
+
|
|
98
|
+
const onDrop = useCallback(
|
|
99
|
+
(event) => {
|
|
100
|
+
event.preventDefault();
|
|
101
|
+
|
|
102
|
+
console.log(event)
|
|
103
|
+
const type = event.dataTransfer.getData('application/reactflow');
|
|
104
|
+
|
|
105
|
+
// check if the dropped element is valid
|
|
106
|
+
if (typeof type === 'undefined' || !type) {
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// project was renamed to screenToFlowPosition
|
|
111
|
+
// and you don't need to subtract the reactFlowBounds.left/top anymore
|
|
112
|
+
// details: https://reactflow.dev/whats-new/2023-11-10
|
|
113
|
+
const position = screenToFlowPosition({
|
|
114
|
+
x: event.clientX,
|
|
115
|
+
y: event.clientY,
|
|
116
|
+
});
|
|
117
|
+
const newNode = {
|
|
118
|
+
id: getId(),
|
|
119
|
+
type,
|
|
120
|
+
position,
|
|
121
|
+
data: { label: `${type} node` },
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
setNodes((nds) => nds.concat(newNode));
|
|
125
|
+
},
|
|
126
|
+
[screenToFlowPosition],
|
|
127
|
+
);
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
// el.appendChild(
|
|
131
|
+
return (
|
|
132
|
+
<div style={{ position: "relative", height: "400px", width: "100%" }}>
|
|
133
|
+
<ReactFlow
|
|
134
|
+
nodes={nodes}
|
|
135
|
+
edges={edges}
|
|
136
|
+
// onNodesChange={onNodesChange}
|
|
137
|
+
// onEdgesChange={onEdgesChange}
|
|
138
|
+
onNodesChange={onNodesChange}
|
|
139
|
+
onEdgesChange={onEdgesChange}
|
|
140
|
+
onConnect={onConnect}
|
|
141
|
+
onDrop={onDrop}
|
|
142
|
+
onDragOver={onDragOver}
|
|
143
|
+
nodeTypes={nodeTypes}
|
|
144
|
+
fitView
|
|
145
|
+
style={rfStyle}>
|
|
146
|
+
<Background variant="dots" gap={12} size={1} />
|
|
147
|
+
<MiniMap />
|
|
148
|
+
<Controls />
|
|
149
|
+
</ReactFlow>
|
|
150
|
+
</div>
|
|
151
|
+
);
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
export default { render };
|