使用CodeMirror实现Python3在线编辑器的示例代码

时间:2022-04-08 02:49:07

一、编写页面

主要是引入相关的css文件和js文件,这里采用简单插入link和script标签的形式。

?
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
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  <title>document</title>
  <link rel="stylesheet" href="codemirror/lib/codemirror.css" rel="external nofollow" >
  <link rel="stylesheet" href="codemirror/addon/fold/foldgutter.css" rel="external nofollow" >
  <link rel="stylesheet" href="codemirror/addon/hint/show-hint.css" rel="external nofollow" >
  <link rel="stylesheet" href="codemirror/addon/lint/lint.css" rel="external nofollow" >
  <link rel="stylesheet" href="leetcode.css" rel="external nofollow" >
</head>
<body>
  <form action="">
    <textarea id="editor" class="editor"></textarea>
  </form> 
  <button id="test">click</button>
</body>
</html>
<script src="codemirror/lib/codemirror.js"></script>
 
<script src="codemirror/addon/comment/comment.js"></script>
 
<script src="codemirror/addon/selection/active-line.js"></script>
 
<script src="codemirror/keymap/sublime.js"></script>
 
<script src="codemirror/addon/hint/show-hint.js"></script>
<script src="codemirror/mode/python/python.js"></script>
 
<script src="codemirror/addon/fold/foldcode.js"></script>
<script src="codemirror/addon/fold/foldgutter.js"></script>
<script src="codemirror/addon/fold/brace-fold.js"></script>
<script src="codemirror/addon/fold/indent-fold.js"></script>
<script src="codemirror/addon/fold/comment-fold.js"></script>
 
<script src="codemirror/addon/edit/closebrackets.js"></script>
<script src="codemirror/addon/edit/matchbrackets.js"></script>
 
<script src="axios.js"></script>
 
<script src="index.js"></script>

二、配置codemirror

在index.js中配置codemirror

?
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
window.onload = function () {
  var el = document.getelementbyid("editor");
  var version = "# version: python3\n\n";
  var codeareatip = "# please edit your code here:\n";
  var codestart = "# code start\n\n";
  var codeend = "# code end\n\n";
  var codetip = "'''\nthis function is the entry of this program and\nit must be return your answer of current question.\n'''\n";
  var code = "def solution():\n\tpass";
  var initvalue = version + codeareatip + codestart + codeend + codetip + code;
  var mycodemirror = codemirror.fromtextarea(el, {
    mode: "python", // 语言模式
    theme: "leetcode", // 主题
    keymap: "sublime", // 快键键风格
    linenumbers: true, // 显示行号
    smartindent: true, // 智能缩进
    indentunit: 4, // 智能缩进单位为4个空格长度
    indentwithtabs: true, // 使用制表符进行智能缩进
    linewrapping: true, //
    // 在行槽中添加行号显示器、折叠器、语法检测器
    gutters: ["codemirror-linenumbers", "codemirror-foldgutter", "codemirror-lint-markers"],
    foldgutter: true, // 启用行槽中的代码折叠
    autofocus: true, // 自动聚焦
    matchbrackets: true, // 匹配结束符号,比如"]、}"
    autoclosebrackets: true, // 自动闭合符号
    styleactiveline: true, // 显示选中行的样式
  });
  // 设置初始文本,这个选项也可以在fromtextarea中配置
  mycodemirror.setoption("value", initvalue);
  // 编辑器按键监听
  mycodemirror.on("keypress", function() {
    // 显示智能提示
    mycodemirror.showhint(); // 注意,注释了codemirror库中show-hint.js第131行的代码(阻止了代码补全,同时提供智能提示)
  });
  var test = document.getelementbyid("test");
  test.onclick = function() {
    var value = mycodemirror.getvalue();
    axios.post("http://localhost/api/runcode", {
      code: value
    }).then(function(res) {
      console.log(res);
    });
  };
};

三、后台调用python shell

过程如下:

  • 在接收的代码字符串后面添加print(solution())用于打印结果
  • 将第一步处理后的字符串写入一个文件中这里是code/code.py
  • 使用child_process模块的exec方法调用shell执行python code/code.py命令,获取打印结果
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
const express = require("express");
const { exec } = require("child_process");
const router = express.router();
router.post("/api/runcode", (req, res) => {
  let code = req.body.code;
  fs.writefile("code/code.py", code+"\nprint(solution())", (err) => {
    let command = "python code/code.py";
    exec(command, (err, stdout, stdin) => {
      if(err){
        let reg = /[\d\d]*(line\s\d)[\d\d]*?(\w*(?:error|exception).*)/im;
        let matcharr = reg.exec(err.message);
        matcharr.shift();
        res.send(matcharr.join(", "));
      }
      else
        res.send(stdout);
    });
  });
});

效果:

使用CodeMirror实现Python3在线编辑器的示例代码

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://segmentfault.com/a/1190000017875558