Skip to content

Commit

Permalink
solved #48
Browse files Browse the repository at this point in the history
  • Loading branch information
yilinxia committed Mar 28, 2023
1 parent 8273327 commit 2bd1c43
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 35 deletions.
16 changes: 2 additions & 14 deletions jupyterlab_nodeeditor/sample_models/export_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,8 @@ models:
- name: PhotosynthesisModel:photosynthesis_rate
language: python
args: ./src/photosynthesis.py
- name: Input_File
outputs:
- name: Input_File:load_file
language: python
args: ./Input/input.txt
- name: Export_File
inputs:
- name: Export_File:export_file
language: python
args: ./output.txt
connections:
- input: python_modelA:outputA
output: python_modelB:inputB
- input: Input_File:load_file
output: python_modelA:inputA
output: PhotosynthesisModel:light_intensity
- input: python_modelB:outputB
output: Export_File:export_file
output: PhotosynthesisModel:temperature
48 changes: 39 additions & 9 deletions jupyterlab_nodeeditor/yaml_editor_yaml.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"id": "89bf320b-3577-4652-a6ce-0f522c1ada51",
"metadata": {},
"outputs": [],
Expand All @@ -28,7 +28,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"id": "80fae6c8-bae0-47b6-9d68-2ca678f3d8f9",
"metadata": {},
"outputs": [],
Expand All @@ -47,14 +47,29 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"id": "3c12c8a2-010c-4497-8a7a-7006a14c4c96",
"metadata": {
"tags": []
},
"outputs": [],
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "d3b9431b8e3a4e0ab2268b9053c291c6",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"AppLayout(children=(Label(value='Node Editor', layout=Layout(grid_area='header')), Accordion(layout=Layout(gri…"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"editor=jlne.yml_trans([sample_3, photosynthesis],show_instance=True)\n",
"editor=jlne.yml_trans([sample_3, photosynthesis])\n",
"editor"
]
},
Expand All @@ -70,7 +85,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 7,
"id": "e8dc9715-80bf-4321-bf7d-655f02e59bd0",
"metadata": {},
"outputs": [],
Expand All @@ -88,7 +103,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 8,
"id": "262c374a-9963-41b5-aebc-39d1dedf059f",
"metadata": {},
"outputs": [],
Expand All @@ -98,10 +113,25 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 9,
"id": "903cb0b8-dc8d-4b3a-bee1-3af4d2296771",
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "61f41dffbe864a82a0676632095b5edb",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"AppLayout(children=(Label(value='Node Editor', layout=Layout(grid_area='header')), Accordion(children=(VBox(ch…"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"editor_test=jlne.yml_trans(export_test, show_instance=True)\n",
"editor_test"
Expand Down
97 changes: 85 additions & 12 deletions jupyterlab_nodeeditor/yggdrasil_to_editor.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import jupyterlab_nodeeditor as jlne
from yggdrasil import yamlfile
import yaml

# import time


Expand Down Expand Up @@ -248,7 +249,7 @@ def editor_yaml(editor, address):
editor.node_editor.sync_config()
editor_json = editor.node_editor.editorConfig
editor_py = editor.node_editor.nodes
editor_py_conn = editor.node_editor.connections
# editor_py_conn = editor.node_editor.connections

if editor_py == []:
return "No component/instance has been detected in the workspace"
Expand All @@ -267,32 +268,104 @@ def editor_yaml(editor, address):
node_id = list(
editor_py[0].controls[0].editor.editorConfig["nodes"].keys()
)[model_pos]
nodes_info = editor_json["nodes"]
else:
node_id = list(
editor_py[0]
.controls[0]
.editor.editorConfig[jupyter_id]["nodes"]
.keys()
)[model_pos]
nodes_info = editor_json[jupyter_id]["nodes"]
py_models_dict[node_id] = single_model
conn_ls = []
for conn in range(len(editor_py_conn)):
single_conn = dict()
input_model_name = editor_py_conn[conn].source_node.title
input_port_name = editor_py_conn[conn].source_node.outputs[0].title
single_conn["input"] = input_model_name + ":" + input_port_name

output_model_name = editor_py_conn[conn].destination_node.title
output_port_name = editor_py_conn[conn].destination_node.inputs[0].title
single_conn["output"] = output_model_name + ":" + output_port_name
conn_ls.append(single_conn)
# store all the information about model name and their corresponding ports (id+name)
node_id_ls = nodes_info.keys()
all_models = dict()
for model_id in range(len(editor_py)):
model_name = editor_py[model_id].title
in_ports = dict()
inputs_ls = editor_py[model_id].inputs
for input_port in inputs_ls:
in_ports[input_port.key] = input_port.title

out_ports = dict()
outputs_ls = editor_py[model_id].outputs
for output_port in outputs_ls:
out_ports[output_port.key] = output_port.title

all_models[model_name] = in_ports
all_models[model_name]["outputs"] = out_ports

# extract all the connections
conn_ls = []
for node_id in node_id_ls:
input_ports = nodes_info[node_id]["inputs"].keys()
output_ports = nodes_info[node_id]["outputs"].keys()

for input_port in input_ports:
connect_in_port = nodes_info[node_id]["inputs"][input_port][
"connections"
]
if len(connect_in_port) >= 1:
single_conn = dict()
source_node_id = str(connect_in_port[0]["node"])
source_node_name = py_models_dict[source_node_id]["model_name"]
source_node_output_id = connect_in_port[0]["output"]
source_node_output_name = all_models[source_node_name]["outputs"][
source_node_output_id
]

destination_node_name = py_models_dict[node_id]["model_name"]
destination_input_name = all_models[destination_node_name][
input_port
]

single_conn["input"] = (
source_node_name + ":" + source_node_output_name
)
single_conn["output"] = (
destination_node_name + ":" + destination_input_name
)
if single_conn not in conn_ls:
conn_ls.append(single_conn)
else:
continue

for output_port in output_ports:
connect_out_port = nodes_info[node_id]["outputs"][output_port][
"connections"
]
if len(connect_out_port) >= 1:
single_conn = dict()
dest_node_id = str(connect_out_port[0]["node"])

source_node_name = py_models_dict[node_id]["model_name"]
source_node_output_id = output_port
source_node_output_name = all_models[source_node_name]["outputs"][
source_node_output_id
]

destination_node_name = py_models_dict[dest_node_id]["model_name"]
destination_input_name = all_models[destination_node_name][
connect_out_port[0]["input"]
]
single_conn["input"] = (
source_node_name + ":" + source_node_output_name
)
single_conn["output"] = (
destination_node_name + ":" + destination_input_name
)
if single_conn not in conn_ls:
conn_ls.append(single_conn)
else:
continue

# print(py_models_dict)
yml_dict = {} # the keywords for yml_dict include "models" and "connections"
model_ls = parse_editor_config(py_models_dict, editor_json)
yml_dict["models"] = model_ls
yml_dict["connections"] = conn_ls
# print(yml_dict)

stream = open(address, "w")
yaml.dump(yml_dict, stream, sort_keys=False)

0 comments on commit 2bd1c43

Please sign in to comment.