Skip to content

Commit c022925

Browse files
Upload
1 parent f209a43 commit c022925

File tree

3 files changed

+250
-0
lines changed

3 files changed

+250
-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,143 @@
1+
---
2+
layout: intro
3+
theme: neversink
4+
color: bowdoin
5+
routerMode: hash
6+
favicon: https://avatars.githubusercontent.com/u/9260792
7+
---
8+
9+
# Importing and Turtle
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+
<twemoji-framed-picture v-drag="[602,83,144,144]" />
18+
<twemoji-turtle v-drag="[722,70,215,215]" />
19+
<twemoji-artist-palette v-drag="[658,212,77,77]" />
20+
<twemoji-paintbrush v-drag="[747,255,54,54]" />
21+
22+
---
23+
layout: section
24+
color: bowdoin
25+
---
26+
27+
### In today's class, we are going to be taking a short break from the fundamentals of programming to explore something more fun and interactive!
28+
29+
<twemoji-star-struck v-drag="[830,397,90,90]" />
30+
31+
---
32+
layout: section
33+
color: bowdoin
34+
---
35+
36+
### So far, our programs have been written in one file
37+
38+
<br>
39+
40+
### This is not super realistic as programs often consist of hundreds, thousands, and even <u>millions</u> of lines of code!
41+
42+
<twemoji-face-screaming-in-fear v-drag="[848,388,90,90,10]" />
43+
44+
---
45+
layout: top-title
46+
color: bowdoin-title
47+
---
48+
49+
:: title ::
50+
51+
# Too much code!
52+
53+
:: content ::
54+
55+
<br>
56+
57+
## It would be almost impossible to maintain a codebase that big in one huge file, _especially_ if multiple people were working on it together!
58+
59+
<br>
60+
<br>
61+
62+
## It makes it very difficult to share / reuse code since we would need to constantly copy-paste it into every new project we have!
63+
64+
<br>
65+
<br>
66+
<br>
67+
<br>
68+
69+
## To fix these issues, we need a way to split our programs up across multiple files and somehow reference them in one another!
70+
71+
<twemoji-angry-face v-drag="[861,288,90,90]" />
72+
73+
---
74+
layout: top-title
75+
color: bowdoin-title
76+
---
77+
78+
:: title ::
79+
80+
# `import` Statements
81+
82+
:: content ::
83+
84+
<br>
85+
86+
# `import` statements allow us to tell Python to include code from other files in our code when we execute our programs!
87+
88+
<br>
89+
90+
### With them, you can split your own programs into multiple, more focused, files and it allows you to reuse those files by referencing them in future programs as a ==library==.
91+
92+
<br>
93+
<br>
94+
95+
## Additionally, you can also use ==libraries== written by other people!
96+
97+
<br>
98+
99+
### Python is actually _very_ good at this and it is why it is so commonly used in web development, data science, AI, and so many other areas!
100+
101+
---
102+
layout: top-title
103+
color: bowdoin-title
104+
---
105+
106+
:: title ::
107+
108+
# `import` Statements in Action
109+
110+
:: content ::
111+
112+
#### We can reference things from these external files by ==qualifying== them with the name of the file (without `.py`) followed by a period and the thing we want to access.
113+
114+
```python {monaco-run} {autorun:true, editorOptions: { lineNumbers:'on', fontSize:14}}
115+
# Here we ask Python to 'include' the built-in math library.
116+
import math # We don't add a ".py" here!
117+
118+
print(f"e is equal to {math.e}")
119+
120+
degrees: float = 270.0
121+
print(f"{degrees}° in radians is {math.radians(degrees) / math.pi}π")
122+
123+
a: int = 30
124+
b: int = 20
125+
print(f"The gcd of {a} and {b} is {math.gcd(a, b)}")
126+
print(f"The lcm of {a} and {b} is {math.lcm(a, b)}")
127+
```
128+
129+
---
130+
layout: section
131+
color: bowdoin
132+
---
133+
134+
# Now, let's look at the `turtle` library!
135+
136+
<br>
137+
138+
### Turtle is a built-in graphics library that allows us to draw things to the screen!
139+
140+
<twemoji-framed-picture v-drag="[611,314,144,144]" />
141+
<twemoji-turtle v-drag="[738,287,215,215]" />
142+
<twemoji-artist-palette v-drag="[686,450,77,77]" />
143+
<twemoji-paintbrush v-drag="[768,475,54,54]" />
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)