Skip to content

Instantly share code, notes, and snippets.

@Pk13055
Created July 3, 2017 23:05
Show Gist options
  • Save Pk13055/dcf8222179f318f03ef2f44a1b539b16 to your computer and use it in GitHub Desktop.
Save Pk13055/dcf8222179f318f03ef2f44a1b539b16 to your computer and use it in GitHub Desktop.
Python Flask App (simple SPA, no modules)
from flask import Flask, request, redirect, url_for, render_template
import sys, os
# this creates our Flask app
app = Flask(__name__)
# define all your routes here
@app.route('/', methods = ['POST', 'GET'])
def home():
if request.method == 'GET':
title = "Main Page"
data = "Hello"
return render_template('main.html.j2', title = title, data = data)
# this is the port assignment and app running; do not change anything here
def main():
port = 8000
try:
port = int(sys.argv[1])
except:
pass
app.run(debug = True, port = port, host = '127.0.0.1')
if __name__ == '__main__':
main()
@Pk13055
Copy link
Author

Pk13055 commented Jul 3, 2017

Simple Python 3.x implementation of a flask app

Instructions

  • All you have to do is run app.py with your preferred python3.x version
    • python3.6 app.py <port>
      • where port is the port on which you want to run it (default is 8000)
  • To view your website, go to any browser and access
    • locahost:<port>/
      • where port is the port you set

Adding models

  • Add models and access them with the class name to gain db access.

@Pk13055
Copy link
Author

Pk13055 commented Jul 3, 2017

You have to additionally create a templates folder that will house all your templates, viz. main.html.j2 and so on. Remember to name them with the .j2 extension so as to have proper indentation and syntax highlighting

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment