想要在网页上运行 Python 代码?这个简单易用的在线代码运行器可以满足你的需求!

  1. 创建 HTML 文件:新建一个名为 index.html 的 HTML 文件,并复制以下代码到文件中:
<!DOCTYPE html>
<html>
<head>
    <title>Python Code Runner</title>
</head>
<body>
    <h1>Python Code Runner</h1>
    <textarea id='code' rows='10' cols='50' placeholder='在这里输入你的 Python 代码'></textarea>
    <button onclick='runCode()'>运行代码</button>
    <h2>运行结果:</h2>
    <pre id='output'></pre>

    <script>
        function runCode() {
            var code = document.getElementById('code').value;
            var output = document.getElementById('output');

            try {
                output.textContent = eval(code);
            } catch (error) {
                output.textContent = error;
            }
        }
    </script>
</body>
</html>
  1. 保存并打开:保存 index.html 文件,并用浏览器打开它。你将看到一个包含文本区域和运行按钮的网页。

  2. 输入代码:在文本区域中输入你想要运行的 Python 代码。

  3. 点击运行:点击“运行代码”按钮,你将在页面下方看到代码的运行结果。

**注意:**这个简单的代码运行器只适用于运行单行 Python 代码。对于多行代码、导入模块等更复杂的功能,建议使用 Python 的 Web 框架,如 Django 或 Flask 来构建更完整的网站。

在线 Python 代码运行器 - 立即体验

原文地址: https://www.cveoy.top/t/topic/o4N9 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录