使用CodeMirror實(shí)現(xiàn)Python3在線編輯器的示例代碼
一、編寫(xiě)頁(yè)面
主要是引入相關(guān)的css文件和js文件,這里采用簡(jiǎn)單插入link和script標(biāo)簽的形式。
<!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
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", // 語(yǔ)言模式
theme: "leetcode", // 主題
keyMap: "sublime", // 快鍵鍵風(fēng)格
lineNumbers: true, // 顯示行號(hào)
smartIndent: true, // 智能縮進(jìn)
indentUnit: 4, // 智能縮進(jìn)單位為4個(gè)空格長(zhǎng)度
indentWithTabs: true, // 使用制表符進(jìn)行智能縮進(jìn)
lineWrapping: true, //
// 在行槽中添加行號(hào)顯示器、折疊器、語(yǔ)法檢測(cè)器
gutters: ["CodeMirror-linenumbers", "CodeMirror-foldgutter", "CodeMirror-lint-markers"],
foldGutter: true, // 啟用行槽中的代碼折疊
autofocus: true, // 自動(dòng)聚焦
matchBrackets: true, // 匹配結(jié)束符號(hào),比如"]、}"
autoCloseBrackets: true, // 自動(dòng)閉合符號(hào)
styleActiveLine: true, // 顯示選中行的樣式
});
// 設(shè)置初始文本,這個(gè)選項(xiàng)也可以在fromTextArea中配置
myCodeMirror.setOption("value", initValue);
// 編輯器按鍵監(jiān)聽(tīng)
myCodeMirror.on("keypress", function() {
// 顯示智能提示
myCodeMirror.showHint(); // 注意,注釋了CodeMirror庫(kù)中show-hint.js第131行的代碼(阻止了代碼補(bǔ)全,同時(shí)提供智能提示)
});
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);
});
};
};
三、后臺(tái)調(diào)用python shell
過(guò)程如下:
- 在接收的代碼字符串后面添加
print(solution())用于打印結(jié)果 - 將第一步處理后的字符串寫(xiě)入一個(gè)文件中
這里是code/code.py - 使用
child_process模塊的exec方法調(diào)用shell執(zhí)行python code/code.py命令,獲取打印結(jié)果
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);
});
});
});
效果:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
opencv resize圖片為正方形尺寸的實(shí)現(xiàn)方法
這篇文章主要介紹了opencv resize圖片為正方形尺寸的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
flask中響應(yīng)錯(cuò)誤的處理及errorhandler的應(yīng)用方式
這篇文章主要介紹了flask中響應(yīng)錯(cuò)誤的處理及errorhandler的應(yīng)用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12
Python 實(shí)現(xiàn)使用空值進(jìn)行賦值 None
這篇文章主要介紹了Python 實(shí)現(xiàn)使用空值進(jìn)行賦值 None,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03
Keras搭建M2Det目標(biāo)檢測(cè)平臺(tái)示例
這篇文章主要為大家介紹了Keras搭建M2Det目標(biāo)檢測(cè)平臺(tái)實(shí)現(xiàn)的源碼示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05
Django查詢數(shù)據(jù)庫(kù)的性能優(yōu)化示例代碼
這篇文章主要給大家介紹了關(guān)于Django查詢數(shù)據(jù)庫(kù)性能優(yōu)化的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-09-09

