使用CodeMirror實現(xiàn)Python3在線編輯器的示例代碼
更新時間:2019年01月14日 14:53:29 作者:小結(jié)點
這篇文章主要介紹了使用CodeMirror實現(xiàn)Python3在線編輯器的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
一、編寫頁面
主要是引入相關(guān)的css文件和js文件,這里采用簡單插入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", // 語言模式
theme: "leetcode", // 主題
keyMap: "sublime", // 快鍵鍵風(fēng)格
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, // 匹配結(jié)束符號,比如"]、}"
autoCloseBrackets: true, // 自動閉合符號
styleActiveLine: true, // 顯示選中行的樣式
});
// 設(shè)置初始文本,這個選項也可以在fromTextArea中配置
myCodeMirror.setOption("value", initValue);
// 編輯器按鍵監(jiān)聽
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);
});
};
};
三、后臺調(diào)用python shell
過程如下:
- 在接收的代碼字符串后面添加
print(solution())用于打印結(jié)果 - 將第一步處理后的字符串寫入一個文件中
這里是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);
});
});
});
效果:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python Threading 線程/互斥鎖/死鎖/GIL鎖
這篇文章主要介紹了Python Threading 線程/互斥鎖/死鎖/GIL鎖的相關(guān)知識,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2019-07-07
flask框架實現(xiàn)連接sqlite3數(shù)據(jù)庫的方法分析
這篇文章主要介紹了flask框架實現(xiàn)連接sqlite3數(shù)據(jù)庫的方法,結(jié)合實例形式分析了flask框架連接sqlite3數(shù)據(jù)庫的具體操作步驟與相關(guān)實現(xiàn)技巧,需要的朋友可以參考下2018-07-07
Pandas的Series結(jié)構(gòu)及常用操作實例
這篇文章主要介紹了Pandas的Series結(jié)構(gòu)及常用操作實例,Series序列,是一種一維的結(jié)構(gòu),類似于一維列表和ndarray中的一維數(shù)組,但是功能比他們要更為強大,Series由兩部分組成:索引index和數(shù)值values,需要的朋友可以參考下2023-07-07
django2+uwsgi+nginx上線部署到服務(wù)器Ubuntu16.04
這篇文章主要介紹了django2+uwsgi+nginx上線部署到服務(wù)器Ubuntu16.04,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-06-06

