Last active
April 9, 2018 11:31
-
-
Save chicagobuss/73c298a3185f7a7abece to your computer and use it in GitHub Desktop.
Examples for using pants build to build a simple flask app
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
python_binary( | |
name='theapp', | |
dependencies=[ | |
'3rdparty/python:Flask', | |
'3rdparty/python:gevent', | |
':theapp_lib' | |
], | |
resources=globs('static/*', 'templates/*'), | |
source='main.py' | |
) | |
python_library( | |
name='theapp_lib', | |
sources=globs('*.py') | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
python_binary( | |
name='theapp', | |
dependencies=[ | |
'3rdparty/python:Flask', | |
'3rdparty/python:gevent', | |
], | |
resources=globs('static/*', 'templates/*'), | |
source=theapp.py, | |
zip_safe=False | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from gevent import monkey; monkey.patch_all() | |
from flask import Flask, jsonify, render_template, request | |
from functools import wraps | |
from optparse import OptionParser | |
import logging, os, sys | |
from gevent.wsgi import WSGIServer | |
TEMPLATE_DIR = os.path.join(os.path.dirname(__file__), 'templates') | |
# simple cacher | |
def cache_for(duration): | |
def decorator(func): | |
memoized_values = defaultdict(lambda: {'last_load':0, 'value':None}) | |
@wraps(func) | |
def wrapper(*args, **kwargs): | |
entry = memoized_values["%s%s" % (args, kwargs)] | |
if entry['last_load'] + duration < time(): | |
entry['last_load'] = time() | |
entry['value'] = func(*args, **kwargs) | |
return entry['value'] | |
return wrapper | |
return decorator | |
####################### | |
# Start the flask stuff | |
app = Flask(__name__, template_folder=TEMPLATE_DIR) | |
@app.route('/') | |
def index(): | |
return render_template('index.html') | |
def main(argv=None): | |
parser = OptionParser("%prog [options]") | |
parser.add_option("-d", "--debug", dest="debug", default=False, action="store_true", help="start in debug mode") | |
options, args = parser.parse_args(sys.argv[1:]) | |
if options.debug: | |
app.run(host='0.0.0.0', port=5000, debug=True) | |
else: | |
http_server = WSGIServer(('', 5000), app) | |
http_server.serve_forever() | |
if __name__ == '__main__': | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can you explain what the exact problem you want to solve?