4 ms·
I really like bottle.py [1]. I've used it for a couple of different projects; although not fundamentally different from web.py or flask, it seems to just "make
by peter_l_downs 15y ago
I really like bottle.py [1]. I've used it for a couple of different projects; although not fundamentally different from web.py or flask, it seems to just "make sense".
[1] http://bottlepy.org/docs/stable/ http://bottlepy.org/docs/stable/
An example from their website:
from bottle import get, post, request
@get('/login') # or @route('/login')
def login_form():
return '''<form method="POST">
<input name="name" type="text" />
<input name="password" type="password" />
</form>'''
@post('/login') # or @route('/login', method='POST')
def login_submit():
name = request.forms.get('name')
password = request.forms.get('password')
if check_login(name, password):
return "<p>Your login was correct</p>"
else:
return "<p>Login failed</p>"
- jtreminio 15y agoPHP guy here, don't know much about other languages, but seeing HTML mixed in with code is usually a bad code smell. Is this type of thing common in Python?
- Sargis 15y agoNo, it's not. He's just showcasing the simplicity of bottle.py
- obtu 15y agoThose micro-frameworks are designed to make a hello-world app very short and to the point. There's one (aspen.io) that uses the ^L character as a section separator to achieve maximum concision: setup, request handling, optional template. No space is wasted on function blocks or explicit registration calls.
- greut 15y agoPHP is about mixing it with other stuff (hence the <?php tag). So PHP smells by definition :-)
- nobleach 15y agoJSP, ASP, CFM, Rails ERB, DJango ALL have the same exact thing. The only difference is PHP tutorials typically have a big block at the top of the "page" for business logic, and then intersperse logic throughout the view. Most of these other frameworks/languages (barring CFM and old ASP) expect you to adhere to some sort of MVC. Saying PHP smells by default is a little less than fair. :-)
- donteflon 15y agoThis is a simple example to make a point about the simplicity of using bottle, a python micro-framework contained in a single file with all the stuff you need for simple web projects (like routing, templates, various utilities and even a test server).
- mhd 15y agoNot as bad as those examples you just saw, but as others already said, those are mainly illustrative. Beyond that, it's a matter of template engines and most frameworks allow you to choose one. Generally you won't see PHP/JSP levels of code, but the more popular engines do allow quite a bit of logic, i.e. don't adhere to StringTemplate-level purity[1]. [1]: http://www.cs.usfca.edu/~parrt/papers/mvc.templates.pdf http://www.cs.usfca.edu/~parrt/papers/mvc.templates.pdf
- masklinn 15y agoFor comparison, here is the equivalent Flask code: from flask import Flask, request app = Flask(__name__) @app.route('/login') def login_form(): return '''<form method="POST"> <input name="name" type="text" /> <input name="password" type="password" /> </form>''' @app.route('/login', methods=['POST']) def login_submit(): name = request.form.get('name') password = request.form.get('password') if check_login(name, password): return "<p>Your login was correct</p>" else: return "<p>Login failed</p>"
- peter_l_downs 15y agoThanks for that. I like both frameworks - in fact, I may use Flask for my next project.