Skip to content

Commit c97594a

Browse files
authored
Merge pull request #433 from MEDomics-UdeS/editor
Implemented Code Editor
2 parents 95977f6 + 22098aa commit c97594a

File tree

9 files changed

+533
-66
lines changed

9 files changed

+533
-66
lines changed

go_server/blueprints/learning/learning.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ var prePath = "learning"
1010
// AddHandleFunc adds the specific module handle function to the server
1111
func AddHandleFunc() {
1212
Utils.CreateHandleFunc(prePath+"/run_experiment/", handleRunExperiment)
13+
Utils.CreateHandleFunc(prePath+"/get_file_content/", handleGetFileContent)
14+
Utils.CreateHandleFunc(prePath+"/save_file_content/", handleSaveFileContent)
1315
Utils.CreateHandleFunc(prePath+"/progress/", handleProgress)
1416
}
1517

@@ -25,6 +27,30 @@ func handleRunExperiment(jsonConfig string, id string) (string, error) {
2527
return response, nil
2628
}
2729

30+
// handleGetFileContent handles the request to run an experiment
31+
// It returns the response from the python script
32+
func handleGetFileContent(jsonConfig string, id string) (string, error) {
33+
log.Println("Running script get_file_content...", id)
34+
response, err := Utils.StartPythonScripts(jsonConfig, "../pythonCode/modules/learning/get_file_content.py", id)
35+
Utils.RemoveIdFromScripts(id)
36+
if err != nil {
37+
return "", err
38+
}
39+
return response, nil
40+
}
41+
42+
// handleSaveFileContent handles the request to run an experiment
43+
// It returns the response from the python script
44+
func handleSaveFileContent(jsonConfig string, id string) (string, error) {
45+
log.Println("Running script save_file_content...", id)
46+
response, err := Utils.StartPythonScripts(jsonConfig, "../pythonCode/modules/learning/save_file_content.py", id)
47+
Utils.RemoveIdFromScripts(id)
48+
if err != nil {
49+
return "", err
50+
}
51+
return response, nil
52+
}
53+
2854
// handleProgress handles the request to get the progress of the experiment
2955
// It returns the progress of the experiment
3056
func handleProgress(jsonConfig string, id string) (string, error) {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@
114114
"primeicons": "^7.0.0",
115115
"primereact": "^9.6.2",
116116
"prismjs": "^1.29.0",
117-
"react-ace": "^10.1.0",
117+
"react-ace": "^14.0.1",
118118
"react-bootstrap-icons": "^1.10.3",
119119
"react-bootstrap-sidebar-menu": "^2.0.3",
120120
"react-complex-tree": "^2.2.2",
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import json
2+
import os
3+
import sys
4+
from pathlib import Path
5+
6+
sys.path.append(
7+
str(Path(os.path.dirname(os.path.abspath(__file__))).parent.parent))
8+
9+
from med_libs.GoExecutionScript import GoExecutionScript, parse_arguments
10+
from med_libs.server_utils import go_print
11+
12+
json_params_dict, id_ = parse_arguments()
13+
go_print("running script.py:" + id_)
14+
15+
16+
17+
class GoExecScriptMerge(GoExecutionScript):
18+
"""
19+
This class is used to execute the merge script
20+
21+
Args:
22+
json_params: The input json params
23+
_id: The id of the page that made the request if any
24+
"""
25+
26+
def __init__(self, json_params: dict, _id: str = None):
27+
super().__init__(json_params, _id)
28+
self.results = {"data": "nothing to return"}
29+
30+
def _custom_process(self, json_config: dict) -> dict:
31+
"""
32+
This function reads the content of a file
33+
34+
Args:
35+
json_config: The input json params
36+
"""
37+
go_print(json.dumps(json_config, indent=4))
38+
39+
try:
40+
# Set local variables
41+
file_path = json_config["filePath"]
42+
43+
# Check if the file exists
44+
if not os.path.exists(file_path):
45+
return {"error": "File not found"}
46+
47+
# Read the file content
48+
with open(file_path, "r") as file:
49+
content = file.read()
50+
51+
# Detect the file extension
52+
prog_lang = os.path.splitext(file_path)[1].lower().replace(".", "")
53+
if prog_lang == "py" or prog_lang == "ipynb":
54+
prog_lang = "python"
55+
elif prog_lang == "md":
56+
prog_lang = "markdown"
57+
elif prog_lang == "txt":
58+
prog_lang = "text"
59+
60+
# Return the file content and extension
61+
return {
62+
"content": content,
63+
"language": prog_lang
64+
}
65+
66+
except Exception as e:
67+
return {"error": str(e)}
68+
69+
script = GoExecScriptMerge(json_params_dict, id_)
70+
script.start()
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import json
2+
import os
3+
import sys
4+
from pathlib import Path
5+
6+
sys.path.append(
7+
str(Path(os.path.dirname(os.path.abspath(__file__))).parent.parent))
8+
9+
from med_libs.GoExecutionScript import GoExecutionScript, parse_arguments
10+
from med_libs.server_utils import go_print
11+
12+
json_params_dict, id_ = parse_arguments()
13+
go_print("running script.py:" + id_)
14+
15+
16+
17+
class GoExecScriptMerge(GoExecutionScript):
18+
"""
19+
This class is used to execute the merge script
20+
21+
Args:
22+
json_params: The input json params
23+
_id: The id of the page that made the request if any
24+
"""
25+
26+
def __init__(self, json_params: dict, _id: str = None):
27+
super().__init__(json_params, _id)
28+
self.results = {"data": "nothing to return"}
29+
30+
def _custom_process(self, json_config: dict) -> dict:
31+
"""
32+
Saves the content to a file
33+
34+
Args:
35+
json_config: The input json params
36+
"""
37+
go_print(json.dumps(json_config, indent=4))
38+
39+
try:
40+
# Set local variables
41+
file_path = json_config["filePath"]
42+
new_content = json_config["content"]
43+
44+
# Check if the file exists
45+
if not os.path.exists(file_path):
46+
return {"error": "File not found"}
47+
48+
# Overwrite the file content
49+
with open(file_path, "w") as file:
50+
file.write(new_content)
51+
52+
return {}
53+
54+
except Exception as e:
55+
return {"error": str(e)}
56+
57+
script = GoExecScriptMerge(json_params_dict, id_)
58+
script.start()

0 commit comments

Comments
 (0)