@gdiannarbor | #GDIA2 | #IntroToPython
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
Flask is a small Python framework for web development
It's easy to get up and running, and to start building your web server!
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello World!"
app.run()
@app.route('/')
def hello():
return "Hello World!"
When you type in a URL in your browser, your computer sends a "request" to the server.
That request tells the server what information you're looking for, and how to return it to you.
This is the code that runs when we request the '/' URL
To run this code, go to localhost:5000 in your browser (after running the hello world file).
You can request data from other URLs on the same server, too!
@app.route('/hi')
def hi():
return "Hi there!"
@app.route('/')
def hello():
return "Hello World!"
Try visiting localhost:5000/hi to check it out!
These URLs are called "endpoints"
Write a function that prints "Hello" to the terminal when you make a request to a specific endpoint!
With Flask, you can get input from the URL.
You just have to tell it what information you want.
@app.route('/hello/<name>')
def say_hello(name):
return 'Hello %s' % name
Example URL: localhost:5000/hi/cam
So far, what we've done doesn't look much like a web page!
Flask comes with the ability to render HTML files into web pages.
Create a "templates" directory in your project.
Copy this code into templates/hello.html
<!doctype html>
<title>Hello from Flask</title>
<h1>Hello World</h1>
Shameless plug: Check out our HTML/CSS class!
Once we've defined our web page, we need to tell Flask to display it
This is also called rendering.
Make a new file called "rendering.py" (outside the templates directory).
from flask import Flask
from flask import render_template
app = Flask(__name__)
@app.route('/')
def hello():
return render_template('hello.html')
With Flask, you can pass variables into your HTML file.
Edit your HTML file to look like this:
<!doctype html>
<title>Hello from Flask</title>
<h1>Hello {{ name }} </h1>
Now, we can change one line in our python file to look like this:
return render_template('hello.html', name='Cam')
And we'll be able to send data from our Python code into our HTML page.
So far, we've only been using GET requests.
GET requests are useful when you want to "get" information from another server.
When you want to send information, you use POST requests
An example of a POST request is when you submit information through a form.
Going back to last section, we'll build a quick form.
<!doctype html>
<form action='/handle_request' method='post'>
<input type="text" id="name" name="name">
<input type="submit">
</form>
You'll need a method like this:
from flask import request
@app.route('/handle_request', methods=['POST'])
def print_name():
name = request.form['name']
return name
Choose among any of these projects:
(Resources available on the next page)
Create a GUI Program | Look at simple_gui.py to get started with an interface using the the tkinter library. Add inputs, buttons, and labels to create a game. Maybe Tic-Tac-Toe? |
Search the Web | python-duckduckgo 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.) |
Encryption | Read about the Caesar Cipher or find a similarly simple encryption mechanism online. You should find the ord() and chr() functions helpful, as well as the modulus operator '%' |
continued on next page...
Command Line Game | 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 Colossal Cave Adventure and Zork |
Python.org Documentation | Official Python Documentation |
Think Python | Online and print book with exercises. |
Learn Python the Hard Way | Online and print book with exercises |
Google's Python Class | Video lectures coupled with exercises |
New Coder | Ideas for slightly larger projects and resources to get you started. Projects include accessing API's, scraping pages, writing IRC bots, and others. |
Girl Develop It | Local workshops, events, and coding sessions |
Not on Slack? bit.ly/gdiaa-slack
@gdiannarbor | #GDIA2 | #IntroToPython