Published on March 10, 2014
IMPORT FLASK AN INTRODUCTION TO PYTHON’S FRIENDLY NEIGHBOURHOOD WEB MICROFRAMEWORK. NEIL GREY NEIL@COMBOMASH.COM COMBOMASH ENTERTAINMENT
AGENDA ❖ INTRODUCTIONS ❖ GETTING STARTED ❖ OVERVIEW OF PYTHON & MVC ❖ WHY FLASK? ❖ HELLO WORLD! ❖ ROUTING ❖ TEMPLATES ❖ INTERACTIVE COMPONENTS ❖ FORMS, REQUESTS, & AJAX ❖ DATABASE INTEGRATION (SQLITE) ❖ LOGGING IN / SESSIONS ❖ EMAIL SUPPORT
INTRODUCTIONS ❖ WHO’S THE LOUD GUY UP FRONT? ❖ PROFESSIONAL PROGRAMMER AT MID-SIZED VISUAL EFFECTS & GAMING COMPANIES FOR 6 YEARS ❖ HEAVY FOCUS ON SYSTEMS THAT PROMOTE EFFECTIVE COMMUNICATION & EFFICIENT PROCESSES ❖ CYCLIST, BIKRAMITE, INDEPENDENT GAME DEVELOPER, OPEN- SOURCE ENTHUSIAST, VEGAN, SELF-DIRECTED INVESTOR, MARATHONER, BUSINESS OWNER ❖ BEEN WORKING WITH FLASK REGULARLY FOR JUST OVER A YEAR AND WITH PYTHON FOR LAST 5 YEARS. EXPERIENCE IN WEB TECH DATING BACK TO MID-90’S. ❖ ASK THE AUDIENCE ❖ WHAT’S YOUR NAME & WHY ARE YOU HERE TODAY? ❖ EXPERIENCE LEVEL IN PYTHON? WEB DEVELOPMENT? nataliedeemachine.com
GETTING STARTED ❖ OVERVIEW OF PYTHON & MVC ❖ WHY FLASK? ❖ HELLO WORLD! ❖ ROUTING ❖ TEMPLATES
LEARN PYTHON IN 10 MINUTES HTTP://WWW.STAVROS.IO/TUTORIALS/PYTHON/
WHY FLASK? ❖ SMALL : QUICK TO LEARN AND MASTER ❖ COMPLETE : YOU CAN USE TO DO SERIOUS APPS ❖ LEAN : A SHELL AND A TEXT EDITOR ARE ENOUGH, NO NEED FOR A CUMBERSOME IDE ❖ IT IS VERY WELL DOCUMENTED ❖ WHY USING A SCRIPTING LANGUAGE LIKE PYTHON FOR A WEB APPLICATION? ❖ ADAPTED LANGUAGE WHICH EASILY SUPPORTS PASTING TOGETHER VARIOUS COMPONENTS (DATABASE, RENDERING, ROUTING, . . . ) ❖ MAKES ITS EASIER TO RELEASE EARLY & OFTEN (DEVOPS) ❖ EASIER TO MAINTAIN & MODIFY ❖ SPEED GOOD ENOUGH FOR MANY USE CASES
FLASK: HELLO WORLD! • RUN SCRIPT VIA COMMAND-LINE: PYTHON RUN.PY • GO TO: 127.0.0.1:5000 TO REVIEW
FLASK: ROUTING Flask uses dynamic pathing to match user requests to server- side responses. This is in direct contrast to a language like PHP which primarily uses directory- based pathing to locate files containing called-upon functionality. This is extremely useful since we can pass variables into our dynamic paths so not to duplicate functionality across multiple files, and still have a human-readable URL.
FLASK: ROUTING Status Code Type 400 Bad Request 401 Unauthorized 402 Payment Required 403 Forbidden 404 Not Found 500 Internal Server Error 501 Not Implemented 503 Service Unavailable There’s a lot to be said for graceful error handling, especially when it comes to publicly accessible web sites. Flask lets you easily custom handle errors as well as throw them when needed.
FLASK: TEMPLATES Templating keeps the logic of your web app separate from its layout and style. Ideally, we want to avoid scenarios where we are processing any HTML within our main Python syntax. VS
FLASK: TEMPLATES Templating is handled by Jinja2, a python-esque framework that allows for control statements and loops to be placed into HTML templates, but discourages full logic processing within HTML blocks. In our current example, we are passing 3 variables through “render_template” on the Python side: article_title, user, & posts. If article_title is provided we prepend it in the document header. We also have a for loop that goes through an array of posts, printing out relevant information on each iteration.
FLASK: TEMPLATES We can use Jinja2's template inheritance feature, which allows us to move the parts of the page layout that are common to all templates and put them in a base template from which all other templates are derived. In this example we’ve split out the display of the posts from the standardized outline of the page. This makes it so that we won’t have to maintain elements like the main menu across multiple files.
INTERACTIVE COMPONENTS ❖ FORMS, REQUESTS, & AJAX ❖ DATABASE INTEGRATION (SQLITE) ❖ LOGGING IN / SESSIONS ❖ EMAIL SUPPORT ❖ DEPLOYMENT
FORMS, REQUESTS, & AJAX Let’s use the free drag-and- drop library dropzone.js to demo what processing a file upload server-side would look like.
BASIC DATABASE SETUP (SQLITE) Let’s setup a super-basic plain-text user database that we can then integrate into a login form. Usually we would integrate this with the “flask_login” package, but for the sake of simplicity we’re going about it at a different approach.
SESSIONS / LOGGING IN
SESSIONS / LOGGING IN This time around we throw sqlite into the mix to check and see if the user exists in the database before setting the session ‘logged_in’ variable.
EMAIL SUPPORT To use flask-mail we need to import it in our init and configure it with the settings we seek. Once we’re up and running there we can call upon that functionality from anywhere else in the application.
Thank You We really appreciate you taking the time to join in on this Flask workshop today! If you have any questions down the line, feel free to contact me at email@example.com Over at ComboMash Entertainment, we’re launching a kickstarter for a game where tiy ommand a crew of oil rigger mercenaries tasked with acquiring rare resources from uncharted planets. Build offensive towers to attack hordes of aliens in an ever changing environment. If you’d like follow along with our progress, join our mailing list at http://www.hextraction.com Our Kickstarter campaign will be launching on April 21st and we’d love to have your support! Many Thanks, Neil Grey firstname.lastname@example.org
Building Modern Python Web Frameworks Using Flask With Neil Grey. Cohort 1 Community Week Day 6 - Flask Workshop with Neil Grey from ComboMash. In Week 7 ...
overview // docs // community // snippets // extensions Flask is a microframework for Python based on Werkzeug, Jinja 2 and good intentions.
The lightweight Flask Python Web framework lets you easily ... to be using a few ... look into building full web applications ...
Full Stack Python explains ... deploy and run a production Python web ... I want to learn how to code a Python web application using a framework.
Course 2: Web Development with Python ... of web development to web scraping to advanced web frameworks like Flask, ... Modern Web Development; Flask ...
Heroku for Python: run web apps and APIs in smart containers using Django, Flask and ... stateless architecture is designed for building services in today ...
Web Framework. Free open source ... Written and programmable in Python. ... deploy and manage application from anywhere using your browser. One web2py ...
Welcome to Flask¶ Welcome to Flask’s documentation. This documentation is divided into different parts. I recommend that you get started with ...
Django is a high-level Python Web framework that encourages rapid development and clean, ... Using Django. Get help with Django and follow announcements.