-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass4.html
378 lines (333 loc) · 18.2 KB
/
class4.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Introduction to Python | Girl Develop It Ann Arbor</title>
<meta name="description" content="This is the official Girl Develop It Core Intro to Python course. The course is meant to be taught in four two-hour sessions. Each of the slides and practice files are customizable according to the needs of a given class or audience.">
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<link rel="shortcut icon" href="favicon.ico">
<link rel="stylesheet" href="css/reveal.min.css">
<link rel="stylesheet" href="css/theme/gdiaa.css" id="theme">
<link rel="stylesheet" href="lib/css/rainbow.css">
<link rel="stylesheet" href="css/print/pdf.css" media="print">
<script src="lib/js/head.min.js"></script>
<!--[if lt IE 9]>
<script src="lib/js/html5shiv.js"></script>
<![endif]-->
</head>
<body>
<div class="reveal">
<!-- Any section element inside of this container is displayed as a slide -->
<div class="slides">
<!-- INTRO -->
<section>
<!-- Opening slide -->
<section>
<img src="images/gdi_logo_badge.png" class="img--bare" height="450px" />
<div class="box--small">
<h3><span class="green">Intro to Python</span><br/><small><span class="blue">Class 4</span></small></h3>
<p><small>@gdiannarbor | #GDIA2 | #IntroToPython</small></p>
</div>
</section>
<section>
<h3>Review</h3>
<ul class="box list--xtall copy--small">
<li>Method calls</li>
<li>Combining lists and dictionaries</li>
<li>Builtins for collections</li>
</ul>
</section>
</section>
<!-- Block 1 - Intro to Classes and OOP - 20 minutes -->
<section>
<section>
<h3>Functions on Dictionaries</h3>
<p></p>
<pre><code contenteditable class=" python">
character = {
'x': 10,
'y': 20,
'health': 100,
}
def injure(character, damage):
character['health'] = character['health'] - damage
if character['health'] < 0:
character['health'] = 0
def heal(character, amount):
character['health'] = character['health'] + amount
if character['health'] > 100:
character['health'] = 100
</code></pre>
</section>
</section>
</section>
<section> <!-- Flask -->
<section>
<h3>Flask</h3>
<p class="box--small copy--small">Flask is a small Python framework for web development</p>
<p class="box--small copy--small">It's easy to get up and running, and to start building your web server!</p>
</section>
<section>
<h3>Flask's "Hello World"</h3>
<pre><code contenteditable class=" python">
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello World!"
app.run()
</code></pre>
</section>
<section>
<h3>Requests</h3>
<pre><code contenteditable class=" python">
@app.route('/')
def hello():
return "Hello World!"
</code></pre>
<p class="box--small copy--small">When you type in a URL in your browser, your computer sends a "request" to the server.</p>
<p class="box--small copy--small">That request tells the server what information you're looking for, and how to return it to you.</p>
<p class="box--small copy--small">This is the code that runs when we request the '/' URL</p>
<p class="box--small copy--small">To run this code, go to <a href='localhost:5000'>localhost:5000</a> in your browser (after running the hello world file).</p>
</section>
<section>
<h3>Fancy URLs</h3>
<p class="box--small copy--small">You can request data from other URLs on the same server, too!</p>
<pre><code contenteditable class=" python">
@app.route('/hi')
def hi():
return "Hi there!"
@app.route('/')
def hello():
return "Hello World!"
</code></pre>
<p class="box--small copy--small">Try visiting <a href='localhost:5000/hi' target='_blank'>localhost:5000/hi</a> to check it out!</p>
<p class="box--small copy--small">These URLs are called "endpoints"</p>
</section>
<section>
<h3>Let's Develop It</h3>
<p class="box--small copy--small">Write a function that prints "Hello" to the terminal when you make a request to a specific endpoint!</p>
</section>
</section> <!-- End Flask Intro -->
<section> <!-- Including Variables -->
<section>
<h3>Variables</h3>
<p class="box--small copy--small">With Flask, you can get input from the URL.</p>
<p class="box--small copy--small">You just have to tell it what information you want.</p>
<pre><code contenteditable class=" python">
@app.route('/hello/<name>')
def say_hello(name):
return 'Hello %s' % name
</code></pre>
<p class="box--small copy--small">Example URL: <a href='localhost:5000/hi/cam' target="_blank">localhost:5000/hi/cam</a></p>
</section>
</section>
<section> <!-- Templating -->
<section>
<h3>Building Web Pages</h3>
<p class="box--small copy--small">So far, what we've done doesn't look much like a web page!</p>
<p class="box--small copy--small">Flask comes with the ability to render HTML files into web pages.</p>
<p class="box--small copy--small">Create a "templates" directory in your project.</p>
<p class="box--small copy--small">Copy this code into templates/hello.html</p>
<pre><code contenteditable class="html">
<!doctype html>
<title>Hello from Flask</title>
<h1>Hello World</h1>
</code></pre>
<p class="box--small copy--small">Shameless plug: Check out our HTML/CSS class!</p>
</section>
<section>
<h3>Displaying Web Pages</h3>
<p class="box--small copy--small">Once we've defined our web page, we need to tell Flask to display it</p>
<p class="box--small copy--small">This is also called rendering.</p>
<p class="box--small copy--small">Make a new file called "rendering.py" (outside the templates directory).</p>
<pre><code contenteditable class="python">
from flask import Flask
from flask import render_template
app = Flask(__name__)
@app.route('/')
def hello():
return render_template('hello.html')
</code></pre>
</section>
<section>
<h3>Templating</h3>
<p class="box--small copy--small">With Flask, you can pass variables into your HTML file.</p>
<p class="box--small copy--small">Edit your HTML file to look like this: </p>
<pre><code contenteditable class="python">
<!doctype html>
<title>Hello from Flask</title>
<h1>Hello {{ name }} </h1>
</code></pre>
</section>
<section>
<h3>Rendering Templates</h3>
<p class="box--small copy--small">Now, we can change one line in our python file to look like this: </p>
<pre><code contenteditable class="python">
return render_template('hello.html', name='Cam')
</code></pre>
<p class="box--small copy--small">And we'll be able to send data from our Python code into our HTML page.</p>
</section>
</section> <!-- End Templating -->
<!-- <section> <!- Sending your own requests ->
<section>
<h3>Sending Requests</h3>
<p class="box--small copy--small">To make it easier to build websites, you can use other people's data!</p>
<p class="box--small copy--small">To do that, we need to learn a bit about the requests module</p>
</section>
</section> -->
<section> <!-- POST requests -->
<section>
<h3>POST Requests</h3>
<p class="box--small copy--small">So far, we've only been using GET requests.</p>
<p class="box--small copy--small">GET requests are useful when you want to "get" information from another server.</p>
<p class="box--small copy--small">When you want to send information, you use POST requests</p>
</section>
<section>
<h3>Forms and Buttons</h3>
<p class="box--small copy--small">An example of a POST request is when you submit information through a form.</p>
<p class="box--small copy--small">Going back to last section, we'll build a quick form.</p>
<!-- Code Example for Form -->
<pre><code contenteditable class="html">
<!doctype html>
<form action='/handle_request' method='post'>
<input type="text" id="name" name="name">
<input type="submit">
</form>
</code></pre>
</section>
<section>
<h3>Handling a POST request in Python</h3>
<p class="box--small copy--small">You'll need a method like this:</p>
<pre><code contenteditable class="python">
from flask import request
@app.route('/handle_request', methods=['POST'])
def print_name():
name = request.form['name']
return name
</code></pre>
</section>
</section> <!-- End POST requests -->
<section> <!-- Project Section -->
<section>
<h3>Let's Develop It</h3>
<p class="box--small copy--small">Choose among any of these projects:<br/><small>(Resources available on the next page)</small></p>
<ul class="copy--xsmall list--tall">
<li><strong>Create a GUI Program</strong>- Write a program that interacts with users with a window,<br/>not the command line.
<li><strong>Search the Web </strong>- Write a program that searches the web<br/>using DuckDuckGo and displays results.
</li>
<li><strong>Encryption </strong>- Write a program that encrypts a string from<br/>user input, or file and is able to decrypt it as well.
</li>
<li><strong>Command Line Game </strong>- Create a simple game that runs inside the terminal.
</li>
</section>
<section>
<h3>Let's Develop It Resources</h3>
<table class="copy--small box">
<tr>
<td style="width: 250px;">Create a GUI Program</td>
<td style="padding-bottom: 20px;">Look at <a href="http://www.gdiannarbor.com/events/intro-python/examples/simple_gui.py">simple_gui.py</a> to get started with an interface using the the tkinter library. Add inputs, buttons, and labels to create a game. Maybe Tic-Tac-Toe?</td>
</tr>
<tr>
<td style="width: 250px;">Search the Web</td>
<td style="padding-bottom: 20px;"><a href="https://github.com/crazedpsyc/python-duckduckgo/">python-duckduckgo</a> library to get started. Download duckduckgo.py and put it in the same directory as your code. Use the query() function it provides to begin. (HINT: Results are often empty, but 'related' list usually has a few hits.)</td>
</tr>
<tr>
<td>Encryption</td>
<td>Read about the <a href="https://en.wikipedia.org/wiki/Caesar_cipher">Caesar Cipher</a> or find a similarly simple encryption mechanism online. You should find the ord() and chr() functions helpful, as well as the modulus operator '%'</td>
</tr>
</table>
<p class="copy--xsmall box--small">continued on next page...</p>
</section>
<section>
<h3>Let's Develop It Resources Continued</h3>
<table class="copy--small box">
<tr>
<td style="width: 250px;">Command Line Game</td>
<td>This might be a text adventure with paragraphs of text followed by a series of choices for the user. A choice maps to another node in the story (another paragraph with choices). You might try storing the paragraphs separately in a text file. The format might be something different, such as a series of "rooms", each with a description, for the user to explore by entering commands such as "go west". Examples of these kinds of games are <a href="https://en.wikipedia.org/wiki/Colossal_Cave_Adventure">Colossal Cave Adventure</a> and <a href="https://en.wikipedia.org/wiki/Zork">Zork</a></td>
</tr>
</table>
</section>
</section>
<section>
<h3>Future Resources</h3>
<table class="box copy--xsmall">
<tr>
<td style="width: 360px;"><a href="http://docs.python.org/3.5/">Python.org Documentation</a></td>
<td style="padding-bottom: 20px;">Official Python Documentation</td>
</tr>
<tr>
<td><a href="http://www.greenteapress.com/thinkpython/html/index.html">Think Python</a></td>
<td style="padding-bottom: 20px;">Online and print book with exercises.</td>
</tr>
<tr>
<td><a href="http://learnpythonthehardway.org/book/">Learn Python the Hard Way</a></td>
<td style="padding-bottom: 20px;">Online and print book with exercises</td>
</tr>
<tr>
<td><a href="https://developers.google.com/edu/python/">Google's Python Class</a></td>
<td style="padding-bottom: 20px;">Video lectures coupled with exercises</td>
</tr>
<tr>
<td><a href="http://newcoder.io/tutorials/">New Coder</a></td>
<td style="padding-bottom: 20px;">Ideas for slightly larger projects and resources to get you started. Projects include accessing API's, scraping pages, writing IRC bots, and others.</td>
</tr>
<tr>
<td><a href="http://girldevelopit.com/">Girl Develop It</a></td>
<td style="">Local workshops, events, and coding sessions</td>
</tr>
</table>
</section>
<section>
<section>
<h3>Questions?</h3>
</section>
<section>
<h3>Wanna chat Python?</h3>
<div class="box">
<p class="box"><a href="https://gdiaa.slack.com/messages/python/details/" target="_blank" class="roll"><span>#python</span></a></p>
<p><small>Not on Slack? <a target="_blank" href="http://bit.ly/gdiaa-slack" class="roll"><span data-title="bit.ly/gdiaa-slack">bit.ly/gdiaa-slack</span></a></small></p>
</div>
</section>
<section>
<img src="images/gdi_logo_badge.png" class="img--bare" height="450px" />
<div class="box--small">
<h3><span class="green">Intro to Python</span></h3>
<p><small>@gdiannarbor | #GDIA2 | #IntroToPython</small></p>
</div>
</section>
</section>
</div>
<footer>
<div class="copyright">
@gdiannarbor | #GDIA2 | #IntroToPython
<a rel="license" href="http://creativecommons.org/licenses/by-nc/3.0/deed.en_US"><img alt="Creative Commons License" style="border-width:0" src="http://i.creativecommons.org/l/by-nc/3.0/80x15.png" /></a>
</div>
</footer>
</div>
<script src="js/jquery.min.js"></script>
<script src="js/reveal.min.js"></script>
<script>
// Full list of configuration options available here:
// https://github.com/hakimel/reveal.js#configuration
Reveal.initialize({
controls: true,
progress: true,
history: true,
rollingLinks: true,
theme: Reveal.getQueryHash().theme, // available themes are in /css/theme
transition: Reveal.getQueryHash().transition || 'default', // default/cube/page/concave/zoom/linear/none
// Optional libraries used to extend on reveal.js
dependencies: [
{ src: 'lib/js/classList.js', condition: function() { return !document.body.classList; } },
{ src: 'plugin/markdown/showdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'plugin/markdown/markdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'plugin/highlight/highlight.js', async: true, callback: function() { hljs.initHighlightingOnLoad(); } },
{ src: 'plugin/zoom-js/zoom.js', async: true, condition: function() { return !!document.body.classList; } },
{ src: 'plugin/notes/notes.js', async: true, condition: function() { return !!document.body.classList; } }
]
});
</script>
</body>
</html>