190 likes | 304 Views
First Indico Workshop. WEB FRAMEWORKS. 29-27 May 2013 CERN. Adrian Mönnich. Framework?. What? Do we have one? Do we need one?
E N D
First Indico Workshop WEB FRAMEWORKS 29-27 May 2013 CERN Adrian Mönnich
Framework? What? Do we have one? Do we need one? A web application framework is a software framework that is designed to support the development of dynamic websites, web applications, web services and web resources. Wikipedia
Indico’s “framework” Completely self-made! That’s a good thing, right? It’s really old (almost ten years!) There were basically no Python Web Frameworks out there Five layers – some of them are obsolete! New things got added on top when necessary Bad legacy code was rarely refactored in the past
Indico’s “framework” Anyway, how does it look like? That’s the initial structure It was improved a bit (mod_wsgi) Still mod_python-like URLs and files Custom wrapper to emulate mod_python Uses exec (also known as eval (“evil”))
Indico’s “framework” htdocs/*.py We have 200 of those files. They just call some RH’s process method. Nothing else. URLs look like whatever.py/create def index(req, **params): return RHCategoryCreation(req).process(params) def create(req, **params): return RHCategoryPerformCreation(req).process(params)
Indico’s “framework” RH* Data validation Permission checks Application logic Pre-/Post-request operations (DB connection)
Indico’s “framework” WP* Can specify additional JS packages Handles non-AJAX tabbed navigation Does this need to be a framework layer?!
Indico’s “framework” W* Useful when templates could not contain any logic Now: Templates support loops, conditionals, etc. W* mostly pass variables from the RH/WP to the template W* matches WTF for a reason
Indico’s “framework” Templates We got rid of the our old template engines in 2011 Now there is Mako <span class="name"> % if canEdit: <input name="name" value="${ id }"> % endif ${ fullName} </span>
What about… Django? It’s really popular! • Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design. • Full-Stack - ORM, Administration, Clean URLs, Templating, Caching, i18n • Very useful for rather simple CRUD-style applications • Indico is huge! We don’t want to rewrite everything! • Indico does not use SQL but ZODB!Django uses a SQL DB by default.
Something Else… Flask! • Flask is a microframework for Python based on Werkzeug, Jinja 2 and good intentions. • Small footprint, no bloat and not very intrusive • Easy to extend – by subclassing or (if really necessary) forking • RESTful request dispatching (includes clean URLs) • Extensive documentation & active community on IRC • Already in use for Indico Mobile
Flask How does it look like? from flask import Flask app = Flask(__name__) @app.route("/") def hello(): return "Hello World!" if __name__ == "__main__": app.run() • Obviously this example is very basic • It won’t be that easy to use Flask in Indico
Indico & Flask What are we going to do? Step 1 • Get rid of the custom WSGI code and replace it with Flask/Werkzeug • Werkzeug is the low-level Web/WSGI toolkit Flask is based on • Replace the mod_python emulation with Flask’s routing system • Use Flask’s URL generation instead of the custom URLHandler code
Indico & Flask What are we going to do? Step 2 • Get rid of the htdocs/*.py files which only call RHs anyway • Instead of routing requests to those files/functions make RHs usable as routing targets directly • How? We’ll see! Most likely with a classmethod or a decorator. • Only internal changes so far • Let’s make sure things still work
Indico & Flask Speaking of decorators… • In Flask it’s very common to use decorators when registering URL routes: @app.route("/event/<int:eid>") defevent(eid): return "You are viewing event #%d" % eid That’s nice, especially for smaller applications! But in Indico it would be a major chaos and cause all kinds of trouble with circular dependencies… So we’ll use app.add_url_rule() directly
Indico & Flask What are we going to do? Step 3 • Switch to clean URLs: • /event/<id>or/event/<id>/timetable • Instead of e.g. conferenceDisplay.py?confId=<id> • Setup 301 (permanent) redirects from the old URLs to the new ones. • This is the first and most prominent change visible to users!
Indico & Flask Additional goals (possibly long-term) • Directly access GET and POST data via the Flask APIs (esp. in new code) • Integrate Indico sessions with the Flask session interface • Get rid of the W* classes • Clean up templates (remove actual Python code) • Maybe convert templates from Mako to Jinja2 (better syntax)
Questions? Adrian Mönnich https://github.com/ThiefMaster