A Sample Approach to your Project

Similar documents
HOW TO FLASK. And a very short intro to web development and databases

Flask Guide. Meher Krishna Patel. Created on : Octorber, 2017 Last updated : May, More documents are freely available at PythonDSP

LECTURE 14. Web Frameworks

LECTURE 14. Web Frameworks

CIS192 Python Programming

Python web frameworks

CIS192 Python Programming

Quick housekeeping Last Two Homeworks Extra Credit for demoing project prototypes Reminder about Project Deadlines/specifics Class on April 12th Resul

web.py Tutorial Tom Kelliher, CS 317 This tutorial is the tutorial from the web.py web site, with a few revisions for our local environment.

Building Web Applications

Building a Python Flask Website A beginner-friendly guide

Your departmental website

A QUICK GUIDE TO PROGRAMMING FOR THE WEB. ssh (then type your UBIT password when prompted)

IEMS 5722 Mobile Network Programming and Distributed Server Architecture Semester 2

CMSC 201 Spring 2017 Lab 01 Hello World

Server-side Development using Python and SQL

CMSC 201 Spring 2018 Lab 01 Hello World

Lecture 10(-ish) Web [Application] Frameworks

Part I. UNIX Workshop Series: Quick-Start

One of the hardest things you have to do is to keep track of three kinds of commands when writing and running computer programs. Those commands are:

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer

Flask-Genshi Documentation

Flask Documentation. Release 0.12

Dreamweaver CS6. Table of Contents. Setting up a site in Dreamweaver! 2. Templates! 3. Using a Template! 3. Save the template! 4. Views!

Quick.JS Documentation

CSE 101 Introduction to Computers Development / Tutorial / Lab Environment Setup

Instructor s Notes Web Data Management Web Client/Server Concepts. Web Data Management Web Client/Server Concepts

TailorDev Contact Documentation

Introduction to Linux for BlueBEAR. January

Installing PHP on Windows 10 Bash and Starting a Local Server

Using IDLE for

CSE 115. Introduction to Computer Science I

MIT AITI Python Software Development Lab DJ1:

About the Tutorial. Audience. Prerequisites. Disclaimer & Copyright. TurboGears

Oregon State University School of Electrical Engineering and Computer Science. CS 261 Recitation 1. Spring 2011

:59:32 PM PST

Technical Note: On The Usage and Development of the AWAKE Web Server and Web Applications

Web Programming with Python and JavaScript

CSCE UVM Hands-on Session-1 Pre-Work

Helsinki 19 Jan Practical course in genome bioinformatics DAY 0

Using the Zoo Workstations

Buzzy Documentation. Release 0. Sebastian Pawluś

DATABASE SYSTEMS. Introduction to web programming. Database Systems Course, 2016

TangeloHub Documentation

Django Part II SPARCS 11 undead. Greatly Inspired by SPARCS 10 hodduc

CSCU9B2 Practical 1: Introduction to HTML 5

Carnegie Mellon. Linux Boot Camp. Jack, Matthew, Nishad, Stanley 6 Sep 2016

Software Development I

CS 246 Winter Tutorial 1

CIS 192: Lecture 11 Databases (SQLite3)

Introduction to Cuda Visualization. Graphical Application Tunnelling on Palmetto

Linux/Cygwin Practice Computer Architecture

mongodb-tornado-angular Documentation

DJOAuth2 Documentation

Getting started with Raspberry Pi (and WebIoPi framework)

Web Development and HTML. Shan-Hung Wu CS, NTHU

Café Soylent Green Chapters 4

LAB #5 Intro to Linux and Python on ENGR

Click Studios. Passwordstate. Remote Session Launcher. Installation Instructions

USQ/CSC2406 Web Publishing

GMusicProcurator Documentation

Bitnami Re:dash for Huawei Enterprise Cloud

Agenda. - Final Project Info. - All things Git. - Make sure to come to lab for Python next week

Introduction to Unix The Windows User perspective. Wes Frisby Kyle Horne Todd Johansen

CS Fundamentals of Programming II Fall Very Basic UNIX

VPS SETUP: What is a VPS? A VPS is a cloud server, running on a virtual machine. You can t run a masternode on your computer itself.

nacelle Documentation

Xerte. Guide to making responsive webpages with Bootstrap

CS 143A. Principles of Operating Systems. Instructor : Prof. Anton Burtsev

24 Writing Your First Script

Git. Charles J. Geyer School of Statistics University of Minnesota. Stat 8054 Lecture Notes

Linux hep.wisc.edu

CSCI 201 Lab 1 Environment Setup

Announcements. 1. Class webpage: Have you been reading the announcements? Lecture slides and coding examples will be posted

Software Installation - Accessing Linux and Checking your Environmental Variables

The Structure of the Web. Jim and Matthew

Forms, CGI. Objectives

HTML 5 Form Processing

SO, ARE YOU READY? HERE WE GO:

Beginning HTML. The Nuts and Bolts of building Web pages.

CROWDCOIN MASTERNODE SETUP COLD WALLET ON WINDOWS WITH LINUX VPS

a. Right-click on the Windows Menu Icon (usually on the bottom left) and select Windows PowerShell (Admin). Run the following command and reboot.

Announcements. 1. Class webpage: Have you been reading the announcements? Lecture slides and coding examples will be posted

CS 1301 Fall 2008 Lab 2 Introduction to UNIX

CS4HS Using Google App Engine. Michael Parker

29-27 May 2013 CERN WEB FRAMEWORKS. Adrian Mönnich

Human-Computer Interaction Design

Git Guide. Meher Krishna Patel. Created on : Octorber, 2017 Last updated : October, More documents are freely available at PythonDSP

Bootstrap-Flask Documentation

IoT with Intel Galileo Gerardo Carmona. makerobots.tk

Developing Ajax Applications using EWD and Python. Tutorial: Part 2

Building a Django Twilio Programmable Chat Application

IRLS 504 Student Webpage Tutorial By: Nicole Pagowsky & Sara Hayden Summer 2008

Exercise sheet 1 To be corrected in tutorials in the week from 23/10/2017 to 27/10/2017

Exercise 1: Basic Tools

15-122: Principles of Imperative Computation

Intermediate Programming, Spring Misha Kazhdan

Adobe Dreamweaver CS5 Tutorial

Tomasz Szumlak WFiIS AGH 23/10/2017, Kraków

CHE3935. Lecture 1. Introduction to Linux

Transcription:

A Sample Approach to your Project An object-oriented interpreted programming language Python 3 :: Flask :: SQLite3 A micro web framework written in Python A public domain, barebones SQL database system Doug McGeehan

Your Development Environment Windows Client PuTTY Xming S&T CLC Machines Pre-installed in CLCs Linux Server Python 3 pip virtualenv

Outline 1. Fire up Xming server 2. PuTTY w/ X forwarding 3. Connect to CLC Linux 4. Firefox: test X forwarding 5. mkdir: A Blank Canvas 6. Python virtualenv 7. Installing Flask 8. Flask: Hello World! 9. Your Own Flask Server 10. Flask: Jinja2 Templating 11. Flask: HTML forms & HTTP requests 12. Beginner s SQLite

1. Fire up Xming server Start menu Type xming Find it Execute program Nothing happens, but it should be running

2. PuTTY w/ X forwarding Start menu Type putty Find it Open program Category >> Connection >> SSH >> X11 Verify box is ticked

3. Connecting to a CLC Linux Machine Select a server rcnnxcs213.managed.mst.edu 01 <= NN <= 40 https://it.mst.edu/services/linux/hostnames/ Verify X fowarding enabled Press Open (or Enter) Username + Password

3. Connecting to a CLC Linux Machine Select a server rcnnxcs213.managed.mst.edu 01 <= NN <= 40 https://it.mst.edu/services/linux/hostnames/ Verify X fowarding enabled Press Open (or Enter) Username + Password

4. Testing X forwarding with Firefox Let s test X fowarding is working. djmvfb@rc05xcs213:~$ Whatever precedes $ is your current working directory ~ is your home directory

4. Testing X forwarding with Firefox Let s test X fowarding is working. djmvfb@rc05xcs213:~$ firefox & Execute this command and immediate go into the background. Benefit: no second PuTTY window

4. Testing X forwarding with Firefox Let s test X fowarding is working. djmvfb@rc05xcs213:~$ firefox & Execute this command and immediate go into the background. Xming Benefit: no second PuTTY window

5. mkdir: A Blank Canvas for your Project djmvfb@rc05xcs213:~$ mkdir cs2300proj Make a directory (folder) for your project

5. mkdir: A Blank Canvas for your Project djmvfb@rc05xcs213:~$ mkdir cs2300proj djmvfb@rc05xcs213:~$ cd cs2300proj/ Navigate into said directory

5. mkdir: A Blank Canvas for your Project djmvfb@rc05xcs213:~$ mkdir cs2300proj djmvfb@rc05xcs213:~$ cd cs2300proj/ djmvfb@rc05xcs213:~/cs2300proj$ ~/cs2300proj is now your current working directory

6. Python virtualenv: Your Development Sandbox ~/cs2300proj$ virtualenv -p python3 venv New python executable in /mnt/dfs/djmvfb/users/ djmvfb/linuxhome/cs2300proj/venv/bin/python Installing setuptools, pip, wheel...done. ~/cs2300proj$ source venv/bin/activate (venv) ~/cs2300proj$

6. Python virtualenv: Your Development Sandbox ~/cs2300proj$ source venv/bin/activate (venv) ~/cs2300proj$ Remember this command and the (venv) prefix. You must be in that directory to execute it.

7. Installing Flask: a Python micro web framework (venv) ~/cs2300proj$ pip install flask Installs flask in your virtual environment Does not install globally on the server Benefit: no administrative privileges needed install libraries of different versions than those already on the system

8. Flask: Hello World! Fire up your favorite editor Protip: learn VIM run.sh: Don t just copy this and paste this. export FLASK_APP=webpage.py export FLASK_DEBUG=1 flask run -h localhost -p 8000 Bash shell environment variables

8. Flask: Hello World! Fire up your favorite editor Protip: learn VIM webpage.py: run.sh: Python library import Don t just copy this and paste this. export FLASK_APP=webpage.py export FLASK_DEBUG=1 flask run -h localhost -p 8000 from flask import Flask app = Flask( name ) Don t just copy this and paste this. @app.route('/') def hello_world(): return 'Hello, World!'

8. Flask: Hello World! Fire up your favorite editor Protip: learn VIM run.sh: Don t just copy this and paste this. export FLASK_APP=webpage.py Python decorator* export FLASK_DEBUG=1 flask run -h localhost -p 8000 webpage.py: from flask import Flask app = Flask( name ) Don t just copy this and paste this. @app.route('/') def hello_world(): return 'Hello, World!' *Advanced Python entity, kind of difficult to custom use for beginners.

8. Flask: Hello World! Fire up your favorite editor Protip: learn VIM webpage.py: run.sh: Don t just copy this and paste this. export FLASK_APP=webpage.py export FLASK_DEBUG=1 Function definition flask run -h localhost -p 8000 from flask import Flask app = Flask( name ) Don t just copy this and paste this. @app.route('/') def hello_world(): return 'Hello, World!'

8. Flask: Hello World! Fire up your favorite editor Protip: learn VIM webpage.py: run.sh: Don t just copy this and paste this. export FLASK_APP=webpage.py export FLASK_DEBUG=1 flask run -h localhost Required -p indentation* 8000 from flask import Flask app = Flask( name ) Don t just copy this and paste this. @app.route('/') def hello_world(): return 'Hello, World!' *Tabs or spaces. Recommended: 4 spaces.

9. Your Own Flask Server (venv) ~/cs2300proj$ chmod +x run.sh Changes the run.sh file to be executable in Linux

9. Your Own Flask Server (venv) ~/cs2300proj$ chmod +x run.sh (venv) ~/cs2300proj$./run.sh Execute the run.sh file that exists in the current working directory

9. Your Own Flask Server (venv) ~/cs2300proj$ chmod +x run.sh (venv) ~/cs2300proj$./run.sh * Serving Flask app "webpage" * Forcing debug mode on * Running on http://localhost:8000/ (Press CTRL+C to quit) * Restarting with stat * Debugger is active! * Debugger PIN: 114-080-928 Only accessible through the local host i.e. from applications running on the Linux box.

9. Your Own Flask Server (venv) ~/cs2300proj$ chmod +x run.sh (venv) ~/cs2300proj$./run.sh * Serving Flask app "webpage" * Forcing debug mode on * Running on http://localhost:8000/ (Press CTRL+C to quit) * Restarting with stat * Debugger is active! * Debugger PIN: 114-080-928 Only accessible through the local host i.e. from applications running on the Linux box.

10. Flask + Jinja2 templates: Dynamic Webpages (venv) ~/cs2300proj$ mkdir templates Make a directory (folder) in the current working directory named templates

10. Flask + Jinja2 templates: Dynamic Webpages (venv) ~/cs2300proj$ mkdir templates (venv) ~/cs2300proj$ mkdir static (venv) ~/cs2300proj$ ls List the contents of the current working directory

10. Flask + Jinja2 templates: Dynamic Webpages (venv) ~/cs2300proj$ mkdir templates (venv) ~/cs2300proj$ mkdir static (venv) ~/cs2300proj$ ls pycache run.sh static templates venv webpage.py Static files: JavaScript, CSS, Images Jinja2 templates: HTML files

10. Flask + Jinja2 templates: Dynamic Webpages templates/index.html: <html lang="en"> <head> <title>something Meaningful</title> <meta charset="utf-8" /> </head> Don t just copy this and paste this. <body> <header><h1>{{ header }}</h1></header> </body> </html> webpage.py: Jinja2 template syntax for variable insertion

10. Flask + Jinja2 templates: Dynamic Webpages templates/index.html: <html lang="en"> <head> <title>something Meaningful</title> <meta charset="utf-8" /> </head> Don t just copy this and paste this. <body> <header><h1>{{ header }}</h1></header> </body> </html> webpage.py: from flask import Flask from flask import render_template Don t just copy this and paste this. app = Flask( name ) @app.route('/') def doesnt_matter_what_you_name_this(): return render_template( 'index.html', header='demo for CS2300' )

10. Flask + Jinja2 templates: Dynamic Webpages templates/index.html: <html lang="en"> <head> <title>something Meaningful</title> <meta charset="utf-8" /> </head> Don t just copy this and paste this. <body> <header><h1>{{ header }}</h1></header> </body> </html> webpage.py: from flask import Flask from flask import render_template Don t just copy this and paste this. app = Flask( name ) @app.route('/') def doesnt_matter_what_you_name_this(): return render_template( 'index.html', header='demo for CS2300' )

10. Flask + Jinja2 templates: Dynamic Webpages If statement: {% if True %} {{ variable }} {% endif %} For loop: <ul> {% for item in list %} <li>{{ item }}</li> {% endfor %} </ul>

11. Flask: HTML forms and HTTP requests templates/index.html:... <body> {% if header %} <header><h1>{{ header }}</h1></header> {% endif %} <form action="{{ url_for( insert ) }}" method="post"> Something: <input type="text" name="form-field"> <input type="submit" value="submit"> </form> </body> If header variable is defined when rendering template, include this HTML code Insert the URL for the insert function. When submit button is pressed, use HTTP POST to send form data to the action URL

11. Flask: HTML forms and HTTP requests templates/index.html:... <body> {% if header %} <header><h1>{{ header }}</h1></header> {% endif %} <form action="{{ url_for( insert ) }}" method="post"> Something: <input type="text" name="form-field"> <input type="submit" value="submit"> </form> </body> webpage.py:... @app.route('/') def index(): return render_template( 'index.html', header='demo for CS2300' ) @app.route('/submit', methods=['post']) def insert(): # more code to come return render_template('index.html')

11. Flask: HTML forms and HTTP requests templates/index.html:... <body> {% if header %} <header><h1>{{ header }}</h1></header> {% endif %} <form action="{{ url_for( insert ) }}" method="post"> Something: <input type="text" name="form-field"> <input type="submit" value="submit"> </form> </body> webpage.py:... @app.route('/') def index(): return render_template( 'index.html', header='demo for CS2300' ) @app.route('/submit', methods=['post']) def insert(): # more code to come return render_template('index.html')

11. Flask: HTML forms and HTTP requests templates/index.html:... <body> <header><h1>{{ header }}</h1></header> <form action="{{ url_for( insert ) }}" method="post"> Something: <input type="text" name="form-field"> <input type="submit" value="submit"> </form> </body> </html> webpage.py:... from flask import request @app.route('/submit', methods=['post']) def insert(): field = request.form['form-field'] return render_template( 'index.html', header=field )

11. Flask: HTML forms and HTTP requests templates/index.html:... <body> <header><h1>{{ header }}</h1></header> <form action="{{ url_for( insert ) }}" method="post"> Something: <input type="text" name="form-field"> <input type="submit" value="submit"> </form> </body> </html> webpage.py:... from flask import request @app.route('/submit', methods=['post']) def insert(): field = request.form['form-field'] return render_template( 'index.html', header=field )

12. Beginner s SQLite import sqlite3 connection = sqlite3.connect( database.db ) cursor = connection.curser()......... connection.commit() cursor.close()

12. Beginner s SQLite :: Create Table cursor = connection.curser() cursor.execute(""" CREATE TABLE Stuff ( form_fields VARCHAR(120) ) """) connection.commit()

12. Beginner s SQLite :: Insert Values cursor = connection.curser() cursor.execute( 'INSERT INTO Stuff VALUES (:val)', {'val': 'something'} ) connection.commit()

12. Beginner s SQLite :: Query for Values cursor = connection.curser() result = cursor.execute(""" SELECT * FROM Stuff """).fetchone() result == ( something,)

12. Beginner s SQLite :: Back to Flask webpage.py: import sqlite3 connection = sqlite3.connect( data.db ) from flask import request @app.route('/submit', methods=['post']) def insert(): field = request.form['form-field'] cursor = connection.cursor() cursor.execute( 'INSERT INTO Stuff VALUES (:val)', {'val': field} ) connection.commit() webpage.py (cont): db_contents = cursor.execute( 'SELECT * FROM Stuff' ).fetchall() return render_template( 'index.html', list_of_stuff=db_contents, header='new Stuff' )

12. Beginner s SQLite :: Back to Flask index.html:... {% if list_of_stuff %} <div> <ul> {% for (val,) in list_of_stuff %} <li>{{ val }}</li> {% endfor %} </ul> </div> {% endif %}... webpage.py (cont): db_contents = cursor.execute( 'SELECT * FROM Stuff' ).fetchall() return render_template( 'index.html', list_of_stuff=db_contents, header='new Stuff' )

12. Beginner s SQLite :: Back to Flask index.html:... {% if list_of_stuff %} <div> <ul> {% for (val,) in list_of_stuff %} <li>{{ val }}</li> {% endfor %} </ul> </div> {% endif %}... webpage.py (cont): db_contents = cursor.execute( 'SELECT * FROM Stuff' ).fetchall() return render_template( 'index.html', list_of_stuff=db_contents, header='new Stuff' )

12. Beginner s SQLite :: Back to Flask index.html:... {% if list_of_stuff %} <div> <ul> {% for (val,) in list_of_stuff %} <li>{{ val }}</li> {% endfor %} </ul> </div> {% endif %}... webpage.py (cont): db_contents = cursor.execute( 'SELECT * FROM Stuff' ).fetchall() return render_template( 'index.html', list_of_stuff=db_contents, header='new Stuff' )

12. Beginner s SQLite :: Back to Flask index.html:... {% if list_of_stuff %} <div> <ul> {% for (val,) in list_of_stuff %} <li>{{ val }}</li> {% endfor %} </ul> </div> {% endif %}... webpage.py (cont): db_contents = cursor.execute( 'SELECT * FROM Stuff' ).fetchall() return render_template( 'index.html', list_of_stuff=db_contents, header='new Stuff' )

That s it. Python / Flask / SQLite aren t required for your projects Everything beyond this lecture is up to your own learning

Further Reading Flask tutorial: http://flask.pocoo.org/docs/1.0/tutorial/ SQLite tutorial: http://www.sqlitetutorial.net/ Jinja2 tutorial: https://realpython.com/blog/python/primer-on-jinja-templating/

Demo Code available on S&T GitLab https://git.mst.edu/djmvfb/cs2300proj djmvfb@rc05xcs213:~$ git clone https://git.mst.edu/djmvfb/cs2300proj.git