Slide 1

Slide 1 text

Playing Blackjack A beginner’s tutorial to Flask and AngularJS

Slide 2

Slide 2 text

Backend The logic and management Frontend What you see and interact with

Slide 3

Slide 3 text

The Backend

Slide 4

Slide 4 text

POST /play { 'handTotal': 5, 'dealerSuiteArr': ['hearts', 'clubs'], 'bankAmount': 900, 'userSuiteArr': ['clubs', 'diamonds'], 'dealerHandArr': [4, 9], 'message': '', 'winAmount': 0, 'userHandArr': [1, 4], 'potAmount': 100, 'loseAmount': 0, 'dealerTotal': 13 }

Slide 5

Slide 5 text

Flask: a Python microframework ● It’s very lightweight. You don’t need a lot of files. ● It’s powerful. You can make a webapp in less than 200 lines. ● Very easy to learn. ● Great for hackathons and small apps with simple requirements.

Slide 6

Slide 6 text

MVC Framework. ● MVC is a really popular design framework for web programming languages! ● Three fundamental parts: ○ Data (model) ○ Display (view) ○ Logic (Controller) ● These 3 parts are independent, which makes everything reusable.

Slide 7

Slide 7 text

The View. The view displays the model data, and sends user actions (e.g. button clicks) to the controller. Think of HTML, CSS, Javascript.

Slide 8

Slide 8 text

The Controller. The point of the controller is to process the data for the view. In your HTML/CSS, you might want elements that change depending on the data. The controller is the coding logic to get that data ready. This is defining a route!

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

Everything as a Function. In Flask, most of the things you will ever need are imported as python libraries. To handle requests, and do all that good MVC stuff, is easy. A lot of it is done for you in these libraries.

Slide 11

Slide 11 text

The Frontend

Slide 12

Slide 12 text

POST /play { 'handTotal': 5, 'dealerSuiteArr': ['hearts', 'clubs'], 'bankAmount': 900, 'userSuiteArr': ['clubs', 'diamonds'], 'dealerHandArr': [4, 9], 'message': '', 'winAmount': 0, 'userHandArr': [1, 4], 'potAmount': 100, 'loseAmount': 0, 'dealerTotal': 13 }

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

AngularJS: a frontend framework ● Client-side MVC ● Allows you to build rich user interfaces in a cleaner way

Slide 15

Slide 15 text

Controller ● Attached to the DOM using `ng-controller` ● Within the controller, we perform business logic for our application

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

Directives ● “Markers” that we add into our HTML for data binding ● In this case, we are playing with `$scope.name`using Angular’s ngBind

Slide 18

Slide 18 text

Building Blackjack

Slide 19

Slide 19 text

The Basic Layout app/ static/ → images, stylesheets, javascript. templates/ → HTML. logic → Blackjack rules. forms → Getting user input. controllers → Linking form data and routes. initialization file → Initial parameters. tmp/ → Random junk. configuration file → Settings and passwords. server file → Running Flask app.

Slide 20

Slide 20 text

Fill out the missing parts. If you run python app.py, you’ll notice that it crashes. We’ve provided most of the work but left out the Flask and Angular controllers. Together, we’ll walk through the missing parts to flush out the application. Follow along at http://goo.gl/m6avdI

Slide 21

Slide 21 text

Extra time? The app works okay, but it looks really plain and the backend could be more robust. Practice your new skills by adding more features and redesigning the application. Need a help getting started? We’ve already set up Bootstrap.

Slide 22

Slide 22 text

@grub_007 @frankjwu