return file('index.html') @handler.get('^/(.*)/?$', produces=tubes.HTML) def get_script(request, sid): global scripts if sid in scripts: return run_script1(*scripts[sid]) return tubes.Response('not found', 404) @handler.post('^/?$', produces=tubes.HTML, accepts=tubes.JSON) def new_script(request, body): global scripts, counter if 'url' not in body or 'code' not in body: return tubes.Response('Invalid request', 500) scripts[str(counter)] = (body['url'], body['code']) counter += 1 return '<a href="/%d">test your script</a>' % (counter - 1,) @handler.post('^/test/?$', produces=tubes.HTML, accepts=tubes.JSON) def test_script(request, body): if 'url' not in body or 'code' not in body: return tubes.Response('Invalid request', 500) return run_script1(body['url'], body['code']) if __name__ == '__main__': tubes.run(handler)
else: return tubes.Response("user not found", 404) # return the generated requests javascript code on that url # (on production you should save it to a file) @handler.get('^/requests.js/?$', produces=tubes.JS) def requests(request): '''return the requests.js file to interact with this API''' return tubes.Response(REQUESTS) # return the generated model javascript code on that url # (on production you should save it to a file) @handler.get('^/model.js/?$', produces=tubes.JS) def model(request): '''return the model.js file to interact with this API''' return MODEL # show the API test page here @handler.get('^/test.html/?$', produces=tubes.HTML) def test(request): '''return a dummy html to play with the API''' return TEST_PAGE REQUESTS = intertubes.generate_requests(handler) MODEL = intertubes.generate_model([User]) TEST_PAGE = intertubes.generate_html_example(handler, ('/files/json2.js', '/model.js')) if __name__ == '__main__': tubes.run(handler, use_reloader=True)
@handler.get('^/callback(.*?)$', produces=tubes.TEXT) def confirm_subscription(request, info): if 'hub.challenge' in request.args: return request.args['hub.challenge'] print 'hub.challenge not present in request.args' @handler.get('^/new-notices/?$', produces=tubes.HTML) def get_new_notices(request): css = h.css('/files/style.css') div = h.div(id='timeline') html = h.html(h.head(h.title('notices'), css), h.body(div)) try: while True: notice = new_notices.get(0, False) div.add(notice_to_html(notice)) except Queue.Empty: pass return str(html) REQUESTS = intertubes.generate_requests(handler) MODEL = intertubes.generate_model([User, Notice]) TEST_PAGE = intertubes.generate_html_example(handler, ('/files/json2.js', '/model.js')) if __name__ == '__main__': tubes.run(handler, port=PORT, use_reloader=True, use_debugger=True)
import tubes handler = tubes.Handler() @handler.get("^/?$", produces=tubes.HTML) def hello_world(handler): return "hello world!" @handler.get("^/fail/?$", produces=tubes.HTML) def fail(handler): return tubes.Response("introductory example fail", 500) @handler.get("^/not-here/?$", produces=tubes.HTML) def not_here(handler): return tubes.Response("nothing to see here, please move along", 404) @handler.get("^/redirect/?$", produces=tubes.HTML) def redirect(handler): return tubes.redirect("/", code=301) # you can make werkzeug reload the server everytime a change is made # also you can enable a debugger useful when developing to inspect exceptions # on the browser tubes.run(handler, use_reloader=True, use_debugger=True)