pocketpy/web/index.html
2025-03-26 20:58:49 +05:30

394 lines
10 KiB
HTML

<!-- Note:
This site was created by modifying code pen https://codepen.io/antonmedv/pen/PoPoGwg
written by 'Anton Medvedev' the license can be found bellow.
LICENSE
-------
Copyright (c) 2022 by Anton Medvedev (https://codepen.io/antonmedv/pen/PoPoGwg)
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE. -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="static/codejar/codejar.js"></script>
<script src="static/codejar/linenumbers.js"></script>
<script src="static/highlight.js/highlight.min.js"></script>
<link rel="stylesheet" href="static/highlight.js/github-dark-dimmed.min.css" id="highlight-theme">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<style>
@import url("https://fonts.googleapis.com/css2?family=Lato:wght@300&family=PT+Mono&display=swap");
.hljs {
display: block;
overflow-x: auto;
padding: .5em;
background: #282a36;
}
.hljs-keyword,
.hljs-selector-tag,
.hljs-literal,
.hljs-section,
.hljs-link {
color: #73cbde;
}
.hljs,
.hljs-subst {
color: #f8f8f2;
}
.hljs-string,
.hljs-title,
.hljs-name,
.hljs-type,
.hljs-attribute,
.hljs-symbol,
.hljs-bullet,
.hljs-addition,
.hljs-variable,
.hljs-template-tag,
.hljs-template-variable {
color: #f1fa8c;
}
.hljs-comment,
.hljs-quote,
.hljs-deletion,
.hljs-meta {
color: #6272a4;
}
.hljs-keyword,
.hljs-selector-tag,
.hljs-literal,
.hljs-title,
.hljs-section,
.hljs-doctag,
.hljs-type,
.hljs-name,
.hljs-strong {
font-weight: bold;
}
.hljs-emphasis {
font-style: italic;
}
:root {
--window-width: 80%;
--editor-bg: #282a36;
--editor-text: #f8f8f2;
--output-bg: #282a36;
--output-text: #ffffff;
--editor-line-highlight: rgba(255, 255, 255, 0.07);
}
body {
background-color: #778abb;
font-family: Lato, sans-serif;
font-weight: 300;
font-size: 15px;
margin: 0;
}
*,
*:before,
*:after {
box-sizing: border-box;
}
*:focus {
outline: none;
}
a,
a:visited,
a:active {
color: black;
}
main {
min-height: 100vh;
display: flex;
align-items: center;
flex-direction: column;
}
.title {
color: #fff;
text-align: center;
font-weight: 300;
font-size: 34px;
margin-top: 20px;
}
.window {
width: var(--window-width);
border-radius: 6px;
box-shadow: 0 8px 12px rgba(0, 0, 0, 0.1);
overflow: hidden;
margin-bottom: 20px;
}
.window .window-header {
height: 25px;
background: Gainsboro;
position: relative;
}
.window .window-header .action-buttons {
position: absolute;
top: 50%;
left: 10px;
margin-top: -5px;
width: 10px;
height: 10px;
background: Crimson;
border-radius: 50%;
box-shadow: 15px 0 0 Orange, 30px 0 0 LimeGreen;
}
#code-editor {
border-bottom-left-radius: 6px;
border-bottom-right-radius: 6px;
box-shadow: 0 2px 2px 0 rgba(0, 0, 0, 0.14), 0 1px 5px 0 rgba(0, 0, 0, 0.12),
0 3px 1px -2px rgba(0, 0, 0, 0.2);
font-family: "PT Mono", monospace;
font-size: 14px;
font-weight: 400;
min-height: 300px;
letter-spacing: normal;
line-height: 20px;
padding: 10px;
resize: none !important;
tab-size: 4;
background-color: var(--editor-bg);
color: var(--editor-text);
}
#run-button {
padding-left: 10px;
padding-right: 10px;
font-weight: bold;
cursor: pointer;
}
#theme-toggle {
padding-left: 10px;
padding-right: 10px;
font-weight: bold;
cursor: pointer;
margin-right: 10px;
}
#code-editor.hljs {
padding: 10px;
}
#output-wrapper {
font-family: "PT Mono", monospace;
width: var(--window-width);
min-height: 50px;
background-color: var(--output-bg);
border-radius: 6px;
padding: 10px;
color: var(--output-text);
margin: 0 !important;
}
#code-output span.error-line {
color: #ec5424;
}
.controls {
font-size: 14px;
position: absolute;
top: 50%;
right: 10px;
margin-top: -10px;
display: flex;
}
.controls>div:first-child>a {
display: inline-block;
width: 40px;
}
.features {
width: 547px;
font-size: 16px;
margin-bottom: 30px;
}
/* VS Code Light+ theme styles */
.editor-light-theme {
--editor-bg: #FFFFFF;
--editor-text: #000000;
--output-bg: #FFFFFF;
--output-text: #000000;
--editor-line-highlight: rgba(0, 0, 0, 0.07);
}
.editor-light-theme .hljs {
background: #FFFFFF;
color: #000000;
}
/* Syntax colors matching VS Code Light+ */
.editor-light-theme .hljs-keyword,
.editor-light-theme .hljs-selector-tag,
.editor-light-theme .hljs-literal {
color: #0000FF; /* Blue for keywords */
}
.editor-light-theme .hljs-string {
color: #A31515; /* Maroon for strings */
}
.editor-light-theme .hljs-comment {
color: #008000; /* Green for comments */
font-style: italic;
}
.editor-light-theme .hljs-number {
color: #098658; /* Green for numbers */
}
.editor-light-theme .hljs-built_in,
.editor-light-theme .hljs-title {
color: #795E26; /* Brown for built-ins */
}
.editor-light-theme .hljs-params {
color: #001080; /* Dark blue for parameters */
}
.editor-light-theme .hljs-function {
color: #000000; /* Black for function names */
}
.editor-light-theme .hljs-meta {
color: #AF00DB; /* Purple for meta tags */
}
</style>
<title>Try Online</title>
</head>
<body id="tryonline-body">
<main>
<br>
<div class="window">
<div class="window-header">
<div class="action-buttons"></div>
<div class="controls">
<div id="theme-toggle">Light</div>
<div id="run-button">Run</div>
</div>
</div>
<div class="window-body">
<div id="code-editor" class="language-python" data-gramm="false"></div>
</div>
</div>
<pre id="output-wrapper"><div id="code-output">...</div></pre>
<br>
</main>
<script>
var Module = {
onRuntimeInitialized: function () {
Module.ccall('py_initialize', null, [], []);
console.log("py_initialize() called");
},
print: function (text) {
code_output.innerText += text + '\n';
},
onabort: function (what) {
code_output.innerText += 'Aborted: ' + what + '\n';
Module.ccall('py_finalize', null, [], []);
console.log("py_finalize() called");
}
};
</script>
<script src="lib/pocketpy.js"></script>
<script>
let code_editor = document.querySelector('#code-editor');
let code_output = document.querySelector('#code-output');
let run_button = document.querySelector('#run-button');
let themeToggle = document.querySelector('#theme-toggle');
let highlightTheme = document.querySelector('#highlight-theme');
let mainElement = document.querySelector('main');
code_editor.textContent = '# A recursive fibonacci function.\ndef fib(n):\n if n < 2:\n return n\n return fib(n-1) + fib(n-2)\n\n# Prints all fibonacci from 0 to 10 exclusive.\nfor i in range(10):\n print(f"fib({i}) = {fib(i)}")\n';
let highlight = withLineNumbers(function (editor) {
editor.textContent = editor.textContent;
hljs.highlightElement(editor);
});
highlight(code_editor);
CodeJar(code_editor, highlight);
// Theme toggle functionality
themeToggle.addEventListener('click', function() {
if (mainElement.classList.contains('editor-light-theme')) {
// Switch to dark theme
mainElement.classList.remove('editor-light-theme');
highlightTheme.href = 'static/highlight.js/github-dark-dimmed.min.css';
themeToggle.textContent = 'Light';
} else {
// Switch to light theme
mainElement.classList.add('editor-light-theme');
highlightTheme.href = 'static/highlight.js/vs.min.css'; // Changed to VS theme
themeToggle.textContent = 'Dark';
}
// Re-highlight the code with the new theme
highlight(code_editor);
});
window.onload = function () {
run_button.onclick = function () {
code_output.innerText = '';
const source = code_editor.textContent;
var ok = Module.ccall(
'py_exec', 'boolean', ['string', 'string', 'number', 'number'],
[source, 'main.py', 0, 0]
);
console.log("py_exec() called");
if (!ok) {
Module.ccall('py_printexc', null, [], []);
Module.ccall('py_clearexc', null, ['number'], [0]);
}
}
}
</script>
</body>
</html>