最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

使用CodeMirror實(shí)現(xiàn)Python3在線編輯器的示例代碼

 更新時(shí)間:2019年01月14日 14:53:29   作者:小結(jié)點(diǎn)  
這篇文章主要介紹了使用CodeMirror實(shí)現(xiàn)Python3在線編輯器的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

一、編寫頁(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)聽
  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

過程如下:

  • 在接收的代碼字符串后面添加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)文章

  • opencv resize圖片為正方形尺寸的實(shí)現(xiàn)方法

    opencv resize圖片為正方形尺寸的實(shí)現(xiàn)方法

    這篇文章主要介紹了opencv resize圖片為正方形尺寸的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(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)用方式

    這篇文章主要介紹了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

    這篇文章主要介紹了Python 實(shí)現(xiàn)使用空值進(jìn)行賦值 None,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧
    2020-03-03
  • Keras搭建M2Det目標(biāo)檢測(cè)平臺(tái)示例

    Keras搭建M2Det目標(biāo)檢測(cè)平臺(tái)示例

    這篇文章主要為大家介紹了Keras搭建M2Det目標(biāo)檢測(cè)平臺(tái)實(shí)現(xiàn)的源碼示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-05-05
  • Python在線和離線安裝第三方庫(kù)的方法

    Python在線和離線安裝第三方庫(kù)的方法

    這篇文章主要介紹了Python在線和離線安裝第三方庫(kù)的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-10-10
  • python路徑的寫法及目錄的獲取方式

    python路徑的寫法及目錄的獲取方式

    今天小編就為大家分享一篇python路徑的寫法及目錄的獲取方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧
    2019-12-12
  • Django查詢數(shù)據(jù)庫(kù)的性能優(yōu)化示例代碼

    Django查詢數(shù)據(jù)庫(kù)的性能優(yōu)化示例代碼

    這篇文章主要給大家介紹了關(guān)于Django查詢數(shù)據(jù)庫(kù)性能優(yōu)化的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-09-09
  • 利用python生成照片墻的示例代碼

    利用python生成照片墻的示例代碼

    這篇文章主要介紹了利用python生成照片墻的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-04-04
  • Python面向?qū)ο箢惖睦^承實(shí)例詳解

    Python面向?qū)ο箢惖睦^承實(shí)例詳解

    這篇文章主要介紹了Python面向?qū)ο箢惖睦^承,結(jié)合實(shí)例形式詳細(xì)分析了Python面向?qū)ο蟪绦蛟O(shè)計(jì)中類的繼承原理、定義、使用方法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下
    2018-06-06
  • Python3+Flask安裝使用教程詳解

    Python3+Flask安裝使用教程詳解

    這篇文章主要介紹了Python3+Flask安裝使用教程詳解,需要的朋友可以參考下
    2021-02-02

最新評(píng)論

利川市| 宁津县| 伊宁县| 亚东县| 宜黄县| 章丘市| 长垣县| 高陵县| 攀枝花市| 合肥市| 庆安县| 无为县| 淮安市| 喀喇沁旗| 铁岭县| 遂川县| 龙南县| 石狮市| 石楼县| 准格尔旗| 阿勒泰市| 新沂市| 巧家县| 揭阳市| 大悟县| 寿宁县| 富蕴县| 鹤岗市| 肇庆市| 郴州市| 菏泽市| 香河县| 大厂| 海原县| 阿图什市| 鲜城| 丹凤县| 普格县| 扎鲁特旗| 登封市| 宾阳县|