侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

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

Python  /  管理员 发布于 7年前   298

一、编写页面

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

<!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="https:/article/codemirror/lib/codemirror.css" rel="external nofollow" >  <link rel="stylesheet" href="https:/article/codemirror/addon/fold/foldgutter.css" rel="external nofollow" >  <link rel="stylesheet" href="https:/article/codemirror/addon/hint/show-hint.css" rel="external nofollow" >  <link rel="stylesheet" href="https:/article/codemirror/addon/lint/lint.css" rel="external nofollow" >  <link rel="stylesheet" href="https:/article/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="https:/article/codemirror/lib/codemirror.js"></script><script src="https:/article/codemirror/addon/comment/comment.js"></script><script src="https:/article/codemirror/addon/selection/active-line.js"></script><script src="https:/article/codemirror/keymap/sublime.js"></script><script src="https:/article/codemirror/addon/hint/show-hint.js"></script><script src="https:/article/codemirror/mode/python/python.js"></script> <script src="https:/article/codemirror/addon/fold/foldcode.js"></script><script src="https:/article/codemirror/addon/fold/foldgutter.js"></script><script src="https:/article/codemirror/addon/fold/brace-fold.js"></script><script src="https:/article/codemirror/addon/fold/indent-fold.js"></script><script src="https:/article/codemirror/addon/fold/comment-fold.js"></script><script src="https:/article/codemirror/addon/edit/closebrackets.js"></script><script src="https:/article/codemirror/addon/edit/matchbrackets.js"></script><script src="https:/article/axios.js"></script><script src="https:/article/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", // 快键键风格    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命令,获取打印结果
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);    });  });});

效果:

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


  • 上一条:
    Python3中lambda表达式与函数式编程讲解
    下一条:
    python 根据时间来生成唯一的字符串方法
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 在python语言中Flask框架的学习及简单功能示例(0个评论)
    • 在Python语言中实现GUI全屏倒计时代码示例(0个评论)
    • Python + zipfile库实现zip文件解压自动化脚本示例(0个评论)
    • python爬虫BeautifulSoup快速抓取网站图片(1个评论)
    • vscode 配置 python3开发环境的方法(0个评论)
    • 近期文章
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • Laravel 11.15版本发布 - Eloquent Builder中添加的泛型(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2018-04
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2022-01
    • 2023-07
    • 2023-10
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客