Skip to content

Commit 1444349

Browse files
Upload
1 parent c022925 commit 1444349

File tree

3 files changed

+192
-0
lines changed

3 files changed

+192
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import { defineCodeRunnersSetup } from '@slidev/types'
2+
import { CodeRunnerOutputs } from '@slidev/types'
3+
4+
export default defineCodeRunnersSetup(() => {
5+
return {
6+
async python(code) {
7+
const results = await godboltPythonRequest(code);
8+
return results;
9+
}
10+
}
11+
})
12+
13+
async function godboltPythonRequest(code: string): Promise<CodeRunnerOutputs> {
14+
try {
15+
// Godbolt API URL for Python 3.12
16+
const apiUrl = 'https://godbolt.org/api/compiler/python312/compile';
17+
18+
// Make an asynchronous POST request to the Godbolt API with the Python code
19+
const response = await fetch(apiUrl, {
20+
method: 'POST',
21+
headers: {
22+
'Content-Type': 'application/json',
23+
'Accept': 'application/json' // We need to explicitly ask for a JSON response, text is the default
24+
},
25+
body: JSON.stringify({
26+
source: code,
27+
options: {
28+
compilerOptions: {"executorRequest": true}, // We want to compile + execute
29+
executeParameters: {},
30+
}
31+
})
32+
});
33+
34+
// Check if the response is successful
35+
if (!response.ok) {
36+
return {
37+
error: `Bad response: ${response.statusText}`
38+
};
39+
}
40+
41+
// Parse the response as JSON
42+
const result = await response.json();
43+
44+
// Extract stdout lines and convert them into CodeRunnerOutputText objects
45+
const stdout = result.stdout.map((line: any) => ({
46+
text: line.text
47+
// Hack to keep leading whitespace
48+
.replace(/\s/g, "‎ ")
49+
.replace(/\t/g, "‎ ")
50+
}
51+
));
52+
53+
// Extract stderr lines and convert them into CodeRunnerOutputText objects
54+
const stderr = result.stderr.map((line: any) => ({
55+
text: line.text
56+
// Hack to keep leading whitespace
57+
.replace(/\s/g, "‎ ")
58+
.replace(/\t/g, "‎ "),
59+
class: 'text-red-500'
60+
}
61+
));
62+
63+
if (stderr.length !== 0) {
64+
return stderr;
65+
} else if (stdout.length !== 0) {
66+
return stdout;
67+
} else {
68+
return {
69+
text: 'No output received'
70+
};
71+
}
72+
} catch (error) {
73+
console.error('Failed to execute Python code:', error);
74+
return {
75+
error: `Failed to execute Python code: ${error.message}`
76+
};
77+
}
78+
}
79+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
---
2+
layout: intro
3+
theme: neversink
4+
color: bowdoin
5+
routerMode: hash
6+
favicon: https://avatars.githubusercontent.com/u/9260792
7+
---
8+
9+
## Slices <twemoji-scissors />
10+
11+
<br>
12+
13+
<hr><br>
14+
15+
Christopher Martin - _Bowdoin College_ <a href="https://bowdoin.edu/" class="ns-c-iconlink"><mdi-open-in-new /></a>
16+
<Email v="[email protected]" />
17+
18+
---
19+
layout: top-title
20+
color: bowdoin-title
21+
---
22+
23+
:: title ::
24+
25+
# Recall we can access parts of strings
26+
27+
:: content ::
28+
29+
<br>
30+
31+
#### We can access individual pieces of data inside a string using the `[]` (brackets)
32+
33+
<br>
34+
35+
#### Remember, we start counting from 0!
36+
37+
<br>
38+
39+
40+
| C | h | r | i | s |
41+
| - | - | - | - | - |
42+
| 0 | 1 | 2 | 3 | 4 |
43+
44+
<br>
45+
46+
```python {monaco-run} {editorOptions: {lineNumbers:'on', fontSize:20}}
47+
name = "Chris"
48+
49+
print(name[0], name[1], name[2], name[3], name[4])
50+
```
51+
52+
---
53+
layout: top-title
54+
color: bowdoin-title
55+
---
56+
57+
:: title ::
58+
59+
# Slices
60+
61+
:: content ::
62+
63+
<br>
64+
65+
#### We can access multiple pieces of data inside a string using the `[]` (brackets) like so:
66+
67+
<br>
68+
69+
## `my_str[start:end]` where `start` is inclusive and `end` is exclusive!
70+
71+
<br>
72+
73+
## `start` defaults to 0 if it is not provided!
74+
75+
<br>
76+
77+
## `end` defaults to the length of the string if it is not provided!
78+
79+
<br>
80+
81+
```python {monaco-run} {editorOptions: {lineNumbers:'on', fontSize:20}}
82+
name = "Christopher Martin"
83+
84+
print(name[0:4])
85+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { defineConfig } from 'unocss'
2+
import { colors } from '@unocss/preset-mini'
3+
4+
export default defineConfig ({
5+
rules: [
6+
['neversink-bowdoin-scheme', {
7+
'--neversink-bg-color': colors['white'],
8+
'--neversink-bg-code-color': colors['gray'][100],
9+
'--neversink-fg-code-color': colors['black'],
10+
'--neversink-fg-color': colors['black'],
11+
'--neversink-text-color': colors['black'],
12+
'--neversink-border-color': colors['gray'][950],
13+
'--neversink-highlight-color': colors['gray'][300],
14+
}],
15+
['neversink-bowdoin-title-scheme', {
16+
'--neversink-bg-color': colors['black'],
17+
'--neversink-bg-code-color': colors['gray'][100],
18+
'--neversink-fg-code-color': colors['black'],
19+
'--neversink-fg-color': colors['black'],
20+
'--neversink-text-color': colors['white'],
21+
'--neversink-border-color': colors['gray'][950],
22+
'--neversink-highlight-color': colors['gray'][300],
23+
}]
24+
],
25+
safelist: [
26+
'neversink-bowdoin-scheme', 'neversink-bowdoin-title-scheme'
27+
]
28+
})

0 commit comments

Comments
 (0)