wunderground_endpoint = 'http://api.wunderground.com/api/{0}/hourly/q/{1}/{2}.json' wunderground_endpoint = wunderground_endpoint.format(app.config['WUNDERGROUND_API_KEY'], 'IL', 'Champaign') @dashboard.route('/') def index(): time=datetime.datetime.now().time().strftime('%I:%M').lstrip('0') return render_template('pages/dashboard.html', image_number=random.randrange(1, 9), time=time) #Query no more than once a minute @dashboard.route('/bus') def bus_schedule(): params = {'key' : app.config['CUMTD_API_KEY'], 'stop_id' : 'GRN4TH', 'count' : '5'} response = requests.get(cumtd_endpoint, params=params) json = response.json() departures = [] for departure in json['departures'] : if departure['trip']['direction'] == 'East': departures.append(departure) return jsonify(departures=departures) #Query no more than once every three minutes @dashboard.route('/weather') def weather(): response = requests.get(wunderground_endpoint) json = response.json() return jsonify(json) app.register_blueprint(dashboard, url_prefix='/dashboard')
light_strip_forwards(colors, delay) else: light_strip_backwards(colors, delay) return render_template('lights/index.html', form=form) def light_strip(colors): return send_request(colors, "color") def clear_strip(): return send_request([black], "color") def light_strip_forwards(colors, delay): return send_request([str(delay)] + colors, "forwards") def light_strip_backwards(colors, delay): return send_request([str(delay)] + colors, "backwards") def send_request(colors, endpoint): payload = {'access_token' : access_token, 'args' : ",".join(colors)} response = requests.post(get_url(endpoint), data = payload) print str(datetime.datetime.now()) + "\t : \t" + str(payload) print str(datetime.datetime.now()) + "\t : \t" + response.text def get_url(endpoint): if endpoint not in ['color', 'forwards', 'backwards', 'animation']: raise NameError(str(endpoint) + " is not a valid endpoint") return domain.format(device, endpoint); app.register_blueprint(lights, url_prefix='/lights')
from app.app_and_db import app from app.articles.models import Post from datetime import datetime from flask import abort, Blueprint, flash, render_template, redirect, url_for articles = Blueprint('articles', __name__, template_folder='templates/articles') @articles.route('/', defaults={'page': 1}) @articles.route('/page/<int:page>') def index(page): articles = Post.query.filter(Post.visibility=="Published").filter(Post.published_date <= datetime.now()).order_by(Post.published_date.desc()).paginate(page, app.config["POSTS_PER_PAGE"]) return render_template('articles/index.html', articles=articles) @articles.route('/<string:slug>') def post(slug): article = Post.query.get_or_404(slug) if article.is_visible(): return render_template('articles/article.html', article=article) return abort(404) app.register_blueprint(articles, url_prefix='/article') app.register_blueprint(articles, url_prefix='/blog') app.register_blueprint(articles, url_prefix='/post')