This commit is contained in:
2023-11-14 16:00:55 +08:00
parent dd23d4184d
commit 6481c0bc49
10 changed files with 73 additions and 30 deletions

View File

@@ -6,12 +6,12 @@ export default defineConfig({
title: "徐越的编程书",
markdown: {
container: {
tipLabel: '提示',
warningLabel: '警告',
dangerLabel: '危险',
infoLabel: '信息',
detailsLabel: '详细信息'
}
tipLabel: "提示",
warningLabel: "警告",
dangerLabel: "危险",
infoLabel: "信息",
detailsLabel: "详细信息",
},
},
themeConfig: {
// https://vitepress.dev/reference/default-theme-config
@@ -28,7 +28,45 @@ export default defineConfig({
{ text: "计算机科学", link: "/cs/00/index.md" },
],
sidebar: {
"/python": [{}],
"/python": [
{
text: "课程须知",
items: [
{ text: "本网站", link: "/python/00/book.md" },
{ text: "自测猫", link: "/python/00/cat.md" },
{ text: "练习册", link: "/python/00/playground.md" },
{ text: "判题狗", link: "/python/00/dog.md" },
],
},
{
text: "第一天",
items: [{ text: "", link: "" }],
},
{
text: "第二天",
items: [{ text: "", link: "" }],
},
{
text: "第三天",
items: [{ text: "", link: "" }],
},
{
text: "第四天",
items: [{ text: "", link: "" }],
},
{
text: "第五天",
items: [{ text: "", link: "" }],
},
{
text: "第六天",
items: [{ text: "", link: "" }],
},
{
text: "第七天",
items: [{ text: "", link: "" }],
},
],
"/oa": [{}],
"/crawler": [{}],
"/cs": [
@@ -49,7 +87,11 @@ export default defineConfig({
{ text: "......", link: "/cs/14/index.md" },
],
},
outlineTitle: '目录',
docFooter: { prev: '上一篇', next: '下一篇' }
outlineTitle: "目录",
sidebarMenuLabel: "侧边栏",
returnToTopLabel: "返回顶部",
darkModeSwitchLabel: "浅色/暗色",
docFooter: { prev: "上一篇", next: "下一篇" },
},
})

View File

@@ -12,11 +12,13 @@ import { asyncRun } from "./py"
interface Props {
modelValue: string
readonly: boolean
lang?: "python" | "c"
}
const props = withDefaults(defineProps<Props>(), {
lang: "python",
readonly: false
})
const { isDark } = useData()
@@ -38,21 +40,26 @@ const styleTheme = EditorView.baseTheme({
})
const input = ref("")
const output = ref("")
const code = ref(props.modelValue)
const output = ref<string[]>([])
const error = ref("")
const code = ref(props.modelValue.trim())
async function run() {
const ev = await asyncRun(code.value, input.value)
output.value = ev.result
output.value = ev.results
error.value = ev.error
}
function reset() {
code.value = props.modelValue
code.value = props.modelValue.trim()
error.value = ""
output.value = []
}
</script>
<template>
<p>代码编辑区</p>
<Codemirror
:disabled="props.readonly"
v-model="code"
indentWithTab
:extensions="[styleTheme, lang, isDark ? oneDark : smoothy]"
@@ -65,12 +72,13 @@ function reset() {
:extensions="[styleTheme, isDark ? oneDark : smoothy]"
:tabSize="4"
/>
<p>结果</p>
<p>{{ output }}</p>
<div :class="$style.actions">
<VPButton :class="$style.run" @click="run" text="运行"></VPButton>
<VPButton @click="reset" theme="alt" text="重置"></VPButton>
</div>
<p v-if="output.length || error">运行结果</p>
<pre v-for="(it, index) in output" :key="index">{{ it }}</pre>
<pre>{{ error }}</pre>
</template>
<style module>
.actions {

View File

@@ -14,7 +14,7 @@ const asyncRun = (() => {
return (python: string, input: string) => {
// the id could be generated more carefully
id = (id + 1) % Number.MAX_SAFE_INTEGER
return new Promise<{ result: string; error: string }>((onSuccess) => {
return new Promise<{ results: string[]; error: string }>((onSuccess) => {
callbacks[id] = onSuccess
worker.postMessage({ python, input, id })
})

View File

@@ -2,13 +2,13 @@ import type { Theme } from "vitepress"
import DefaultTheme from "vitepress/theme"
import BVideo from "./components/BVideo.vue"
import Author from "./components/Author.vue"
import CodeEditor from "./components/CodeEditor.vue"
// import CodeEditor from "./components/CodeEditor.vue"
export default {
extends: DefaultTheme,
async enhanceApp({ app }) {
app.component("bvideo", BVideo)
app.component("author", Author)
app.component("code-editor", CodeEditor)
// app.component("code-editor", CodeEditor)
},
} satisfies Theme

View File

@@ -18,12 +18,12 @@ self.onmessage = async (event) => {
const { id, python, input } = event.data
// Now is the easy part, the one that is similar to working in the main thread:
try {
let result = ""
let results = []
self.pyodide.setStdin({ stdin: () => input })
self.pyodide.setStdout({ batched: (str) => (result = str) })
self.pyodide.setStdout({ batched: (str) => results.push(str) })
await self.pyodide.runPythonAsync(python)
self.postMessage({ result, error: '', id })
self.postMessage({ results, error: "", id })
} catch (error) {
self.postMessage({ result: '', error: error.message, id })
self.postMessage({ results: [], error: error.message, id })
}
}

0
docs/python/00/book.md Normal file
View File

1
docs/python/00/cat.md Normal file
View File

@@ -0,0 +1 @@
# 编程规范

0
docs/python/00/dog.md Normal file
View File

View File

View File

@@ -1,8 +0,0 @@
# python
<script setup>
const code = `n = input()
print(n)`
</script>
<code-editor v-model="code"></code-editor>