使用CodeMirror實(shí)現(xiàn)Python3在線編輯器的示例代碼
一、編寫頁(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é)果 - 將第一步處理后的字符串寫入一個(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)文章
python常見(jiàn)統(tǒng)計(jì)分析處理函數(shù)解讀
這篇文章主要介紹了python常見(jiàn)統(tǒng)計(jì)分析處理函數(shù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07Python Threading 線程/互斥鎖/死鎖/GIL鎖
這篇文章主要介紹了Python Threading 線程/互斥鎖/死鎖/GIL鎖的相關(guān)知識(shí),本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-07-07flask框架實(shí)現(xiàn)連接sqlite3數(shù)據(jù)庫(kù)的方法分析
這篇文章主要介紹了flask框架實(shí)現(xiàn)連接sqlite3數(shù)據(jù)庫(kù)的方法,結(jié)合實(shí)例形式分析了flask框架連接sqlite3數(shù)據(jù)庫(kù)的具體操作步驟與相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2018-07-07Pandas的Series結(jié)構(gòu)及常用操作實(shí)例
這篇文章主要介紹了Pandas的Series結(jié)構(gòu)及常用操作實(shí)例,Series序列,是一種一維的結(jié)構(gòu),類似于一維列表和ndarray中的一維數(shù)組,但是功能比他們要更為強(qiáng)大,Series由兩部分組成:索引index和數(shù)值values,需要的朋友可以參考下2023-07-07Python對(duì)接 xray 和微信實(shí)現(xiàn)自動(dòng)告警
xray 是從長(zhǎng)亭洞鑒核心引擎中提取出的社區(qū)版漏洞掃描神器,支持主動(dòng)、被動(dòng)多種掃描方式,自備盲打平臺(tái)、可以靈活定義 POC,功能豐富,這篇文章主要介紹了對(duì)接 xray 和微信實(shí)現(xiàn)自動(dòng)告警,需要的朋友可以參考下2019-09-09Python實(shí)現(xiàn)視頻下載與合成的示例代碼
這篇文章主要為大家詳細(xì)介紹了Python是如何實(shí)現(xiàn)視頻的下載以及合成的,文中的實(shí)現(xiàn)步驟講解詳細(xì),感興趣的小伙伴快跟隨小編一起學(xué)習(xí)一下吧2022-04-04django2+uwsgi+nginx上線部署到服務(wù)器Ubuntu16.04
這篇文章主要介紹了django2+uwsgi+nginx上線部署到服務(wù)器Ubuntu16.04,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-06-06