revealOptions | ||
---|---|---|
|
- Activate your
smiles
virtualenv - Open
smiles/
in your editor. - Open your terminal and navigate to the
smiles/
directory.
- What are some ways the client communicates information to the server?
- What are some ways the server communicates information to the client?
/hello_path/Casey/
Add this to main/urls.py
# main/urls.py
urlpatterns = [
# ...
path('hello_path/<name>/', views.hello_path),
# ...
]
Path variables get passed as arguments to view functions.
# main/views.py
def hello_path(request, name):
return render(request, 'hello.html', context={'name': name})
Then visit http://localhost:8000/hello_path/Casey/
http://localhost:8000/hello_path/Casey/
What if there's no <name>
in the URL?
The desired behavior:
/hello_path/
/hello_path/
Add a path in main/urls.py
to hook up hello_path/
to the hello_path
view.
Watch out! The urlpatterns
list is ordered by precedence.
In other words, earlier items in the list will match before later items.
# main/urls.py
urlpatterns = [
# ...
path('hello_path/', views.hello_path),
path('hello_path/<name>/', views.hello_path),
# ...
]
/hello_path/
To fix this we need to learn about default arguments.
# main/views.py
def hello_path(request, name):
return render(request, 'hello.html', context={'name': name})
Default arguments are optional.
def greet(name, time='morning'):
return 'Good ' + time + ', ' + name
greet('Quinn') # Good morning, Quinn
greet('Quinn', 'afternoon') # Good afternoon, Quinn
greet('Quinn', time='night') # Good night, Quinn
Add 'World'
as the default argument for name
.
def hello_path(request, name='World'):
return render(request, 'hello.html', context={'name': name})
/hello_path/
def yo(name):
return 'Yo ' + name
def test_yo():
assert greet('Casey') == 'Yo Casey'
assert greet('Henrique') == 'Yo Henrique'
Run the tests in tests/test_coin.py
.
pytest tests/test_coin.py
Fix the test_flip
test.
# tests/test_coin.py
def test_flip():
result = coin.flip()
assert result in ['H', 'T']
Then rerun the tests.
pytest tests/test_coin.py
Tip: Use the up arrow to get the previous command in your terminal.
- Write the test first and watch it fail.
- Write the code to make the test pass.
- Improve the code (refactor).
Open tests/test_coin.py
.
flip_loaded()
will always return 'H'
.
Write an assertion for test_flip_loaded()
.
# tests/test_coin.py
def test_flip_loaded():
assert coin.flip_loaded() == 'H'
Run the tests.
pytest tests/test_coin.py
Failure is expected.
Now fix flip_loaded()
.
# main/coins.py
# ...
def flip_loaded():
return 'H'
Run the tests.
pytest tests/test_coin.py
Build an app that allows the user to "say" things (send input) to a character. The app display's the character's face based on the sentiment of the user's input.
A character has 5 "moods" which are associated with a face and a color.
- Very negative, :'-( , dark-red
- Slightly negative, :-( , red
- Neutral, :-| , lightest-blue
- Slightly positive, :-) , washed-green
- Very positive, :-D , green
First step: Design a character. Let's start with the Classic
character.
Run the tests.
pytest tests/test_classic.py
These tests will serve as a guide.
get_face()
has already been implemented.
Now implement get_color()
to make test_get_color()
pass.
Hint: get_color()
should look very similar to get_face()
.
Implement get_color()
.
- Very negative, :'-( , dark-red
- Slightly negative, :-( , red
- Neutral, :-| , lightest-blue
- Slightly positive, :-) , washed-green
- Very positive, :-D , green
pytest tests/test_classic.py
Next let's implement listen(text)
.
We'll use the TextBlob package to compute the sentiment of text.
"Sentiment polarity" measures the positivity/negativity of text.
Here's how to get it with TextBlob
from textblob import TextBlob
good_blob = TextBlob('good day!')
good_blob.sentiment.polarity # 0.875
bad_blob = TextBlob('bad day!')
bad_blob.sentiment.polarity # -0.875
The listen(text)
method should change the character's mood to
the sentiment polarity of text
. If text
is None
or ''
,
reset the mood
to 0.0
.
Here's the start:
def listen(self, text):
# If text is empty, reset mood to 0
if not text:
self.mood = 0.0
Use TextBlob to compute sentiment polarity.
from textblob import TextBlob
good_blob = TextBlob('good day!')
good_blob.sentiment.polarity # 0.875
bad_blob = TextBlob('bad day!')
bad_blob.sentiment.polarity # -0.875
pytest tests/test_classic.py
If you get ahead, try implementing Bear
or Bird
. Run pytest tests/test_more_characters.py
to test them.
- Run your server with
python manage.py runserver
and go to http://localhost:8000 in your browser - Edit
main/urls.py
to hook up the root (''
) and'<character_id>/'
to thecharacter_page
view. - Edit
templates/character.html
to display the face in the right place.
{# templates/character.html #}
<h1 class="f-6 tc sans-serif">
{{ face }}
</h1>
# main/urls.py
urlpatterns = [
# ...
path('', views.character_page),
path('<character_id>/', views.character_page),
]
Now visit http://localhost:8000/ or http://localhost:8000/classic/
Now implement the full Bear
class.
It should have the same behavior as Classic
except with different faces.
Run the following tests.
pytest tests/test_more_characters.py
Tip: You'll need to open another terminal window in order to run your tests.
- In
templates/character.html
, uncomment the link to/bear/
.
{# TODO: Uncomment these after you've implemented Bear and Bird #}
<a class="link grow black-80 hover-blue f6 f5-ns dib mr3" href="/bear/" title="About">Bear</a>
- Edit
main/views.py
to use theBear
class whenbear/
is passed in the URL.
Classic
and Bear
have a lot of the same behavior.
How can we reduce the duplicated code?
Implement a Character
class from which Classic
and Bear
both inherit.
Hint: Use class variables to store the faces.
class Character:
# ...
def get_face(self):
if self.mood < -0.5: # very negative
return self.very_negative
# ...
class Classic(Character):
very_negative = ":'-("
slightly_negative = ':-('
# ...
Before you close your terminal, don't forget to shut down your
development server using ctrl-c
.
Implement Bird
and hook it up to /bird/
.
Read the character_page
view.
Can you understand its logic?
Learn about Python's @property
syntax
and refactor get_face()
and get_color
as properties.
Don't forget to change the tests!
- Learn HTML/CSS/JS
- MDN Learn Web Development
- GDI classes (Intro to HTML/CSS, Intro to JS)
- Build an app that uses a database
- Go through the official Django Tutorial
- See what else Python can do
- Learn git
- Try Git - 15 min tutorial
- Deploy your app
- Follow these instructions for deploying to Heroku
- Learn more about testing