Skip to content

Commit dba2db0

Browse files
Upload
1 parent 0ef0e0e commit dba2db0

File tree

3 files changed

+332
-0
lines changed

3 files changed

+332
-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,225 @@
1+
---
2+
layout: intro
3+
theme: neversink
4+
color: bowdoin
5+
routerMode: hash
6+
favicon: https://avatars.githubusercontent.com/u/9260792
7+
---
8+
9+
## Booleans and Asking Questions <twemoji-red-question-mark />
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+
# Booleans
26+
27+
:: content ::
28+
29+
<br>
30+
31+
# A boolean or `bool` is a ==primitive== data type that is only represented by either `True` or `False`.
32+
33+
<br>
34+
35+
#### In Python, they are always capitalized: `True` and `False`.
36+
37+
<br>
38+
39+
```python {monaco-run} {autorun:true, editorOptions: { lineNumbers:'on', fontSize:16}}
40+
# Assigning Boolean literals to variables
41+
is_snowing: bool = True
42+
is_sunny: bool = False
43+
44+
print("Is it snowing? ⛄", is_snowing)
45+
print(f"Is it sunny? 🌞 {is_sunny}")
46+
```
47+
48+
---
49+
layout: section
50+
color: bowdoin
51+
---
52+
53+
### Okay, but how often will I use `True` or `False`?
54+
55+
<br>
56+
57+
#### By themselves? Probably not very often.. However, `True` and `False` are used heavily because they are the result of a new type of expression!
58+
59+
<twemoji-thinking-face v-drag="[825,376,90,90]" />
60+
61+
---
62+
layout: top-title
63+
color: bowdoin-title
64+
---
65+
66+
:: title ::
67+
68+
# Relational Operators
69+
70+
:: content ::
71+
72+
## Relational operators allow us to ask basic questions about the state of our program's data. Here are the questions we can ask:
73+
74+
<br>
75+
76+
| Human Operator | Python Symbol | Example Expression |
77+
| :----------------------: | :-----------: | :----------------: |
78+
| Equal to | `==` | `5 == 5.0` |
79+
| Not equal to | `!=` | `True != False` |
80+
| Greater than | `>` | `7.0 > 3` |
81+
| Less than | `<` | `"A" < "B"` |
82+
| Greater than or equal to | `>=` | `4 >= 4` |
83+
| Less than or equal to | `<=` | `3.5 <= 5.0` |
84+
85+
---
86+
layout: top-title
87+
color: bowdoin-title
88+
---
89+
90+
:: title ::
91+
92+
# Relational Operators
93+
94+
:: content ::
95+
96+
<br>
97+
98+
## The result of these relational expressions will be a `bool` value.
99+
100+
<br>
101+
102+
## This allows us to store the result of asking a question and potentially use that answer later in our program.
103+
104+
<br>
105+
106+
```python {monaco-run} {autorun:true, editorOptions: { lineNumbers:'on', fontSize:16}}
107+
word_1: str = "Apple"
108+
word_2: str = "Banana"
109+
110+
in_order: bool = word_1 < word_2
111+
112+
print(f"{word_1} comes before {word_2}? {in_order}")
113+
```
114+
---
115+
layout: section
116+
color: bowdoin
117+
---
118+
119+
### Okay, so we can get meaningful `bool` values..
120+
121+
<br>
122+
123+
### But what can we actually do with them?
124+
125+
<twemoji-thinking-face v-drag="[825,376,90,90]" />
126+
127+
---
128+
layout: top-title
129+
color: bowdoin-title
130+
---
131+
132+
:: title ::
133+
134+
# The `if` Statement
135+
136+
:: content ::
137+
138+
#### An `if` statement, also called a "conditional" statement, allows us to ask questions about the state of data and *"conditionally"* execute code based the result.
139+
140+
<br>
141+
142+
#### `if` statements can evaluate <u>any</u> `True` or `False` expression!
143+
144+
<br>
145+
146+
#### Any indented code under the if statement only executes if the expression (also called the condition) evaluates to `True`. If the condition is `False`, the code doesn't run!
147+
148+
149+
```python {monaco-run} {autorun:true, editorOptions: { lineNumbers:'on', fontSize:14}}
150+
temperature: float = 31.5 # Try changing the temp to see what happens!
151+
152+
if temperature < 32.0:
153+
print("It is freezing! 🥶")
154+
print("Maybe it will snow! ⛄")
155+
print("Not inside of the if! 🤪")
156+
```
157+
158+
---
159+
layout: top-title
160+
color: bowdoin-title
161+
---
162+
163+
:: title ::
164+
165+
# The `else` Clause
166+
167+
:: content ::
168+
169+
#### We can optionally add an `else` clause to `if` statements that defines alternative code to runs in the event that the `if` statement's condition evaluates to `False`.
170+
171+
<br>
172+
173+
#### Each `if` statement can only have one `else` clause.
174+
175+
<br>
176+
177+
#### An `else` must be at the same indentation level as its corresponding `if` statement.
178+
179+
<br>
180+
181+
```python {monaco-run} {autorun:true, editorOptions: { lineNumbers:'on', fontSize:14}}
182+
password: str = "1234"
183+
184+
if password == "1101S25":
185+
print("Welcome to our CodeRunner! ✅")
186+
else:
187+
print("That password is incorrect! ❌")
188+
print("Please try again!")
189+
```
190+
191+
---
192+
layout: top-title
193+
color: bowdoin-title
194+
---
195+
196+
:: title ::
197+
198+
# Nesting
199+
200+
:: content ::
201+
202+
<twemoji-nesting-dolls v-drag="[854,86,90,90]" />
203+
204+
#### We can ==nest== conditional statements inside of other conditional statements!
205+
206+
<br>
207+
208+
#### We can also put conditional statements inside of functions!
209+
210+
<br>
211+
212+
```python {monaco-run} {autorun:true, editorOptions: { lineNumbers:'on'}}
213+
def check_number(num: int) -> bool:
214+
if num == 0:
215+
return f"{num} is zero!"
216+
else:
217+
if num > 0:
218+
return f"{num} is positive!"
219+
else:
220+
return f"{num} is negative!"
221+
222+
print(check_number(10))
223+
print(check_number(-5))
224+
print(check_number(0))
225+
```
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)