I imagine it's a limitation of Ruby vs Python for creating DSLs, but a basic Flask app just seems so verbose. It's like seeing "Hello World" in Java vs "Hello World" in a dynamic language like python or ruby.
I imagine it's a limitation of Ruby vs Python for creating DSLs, but a basic Flask app just seems so verbose. It's like seeing "Hello World" in Java vs "Hello World" in a dynamic language like python or ruby.
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello World!"
if __name__ == "__main__":
app.run()
Sinatra: require 'sinatra'
get '/hi' do
"Hello World!"
endAnd that a Flask app is a proper WSGI application (as opposed to a self-running Sinatra handler which has to be quite significantly modified to be Rack-compatible)
As a result, the actual verbosity difference comes down to:
@app.route("/")
def hello():
return "Hello World!"
versus get '/hi' do
"Hello World!"
end
So, as usual, Python not having multiline anonymous functions: for this very case you could write app.route('/')(
lambda: "Hello World!")
but that kind-of looks like shit and you're hosed if you want to add more stuff.(one could argue that the Flask version lets you formally write docstrings which standard Python doc tools will understand)
(and if you're wondering, yes Flask could provide support for not needing to define an app or run it for this trivial example, leaving named functions as the only actual overhead over Sinatra. I don't think there's much value in it, but it wouldn't be very hard)
Huh? Here's the `config.ru` that you need:
$: << File.dirname(__FILE__)
require 'app' #assuming app.rb
run Sinatra::Application
Note that I have no stake in the verbosity claims, as it's not something I particularly care about. But I wouldn't consider this a significant modification.Really? I remember it being significantly more work in the past, like having to change the `require` and create a class around all the routing methods
If that's all which is required to make a Sinatra app into a good Rack citizen, then consider my previous assertion wrong and rescinded.
The intention wasn't to diss Flask. It is a very concise and useful framework. All the comments apply to my original comparison.
public static void main(..){
System.out.println("Hello World");
}
really isn't much more verbose than print "Hello World"
yet every tutorial on a new language points out how frustrating it is for someone learning the language to type "public" "static" "void" "System" and other keywords/objects/modules without having any idea what they mean just to do something simple.In Flask I need to instantiate the Flask app, define a function to run and decorate it with a route. That raises all sorts of questions about what a decorator is (which for a casual pythonist may or may not be something they understand - I know my understanding of decorators is sketchy at best). Then I need to explicitly call app.run() somewhere.
In Sinatra, the DSL makes it much simpler. If I receive a "get '/'" request, then "do" the following stuff. "end".
Flask is very concise. No doubt about it. It's stripped down to it's very minimum. Yet Sinatra manages to be even simpler, which is impressive to me.
I hereby dub this the programmer equivalent of "sharp knees".
Explanation from the ever inimitable Urban Dictionary:
"A judgment leveled against an otherwise perfectly proportioned, if not slightly trim, and pleasant looking woman. Generally uttered by a male who is way below her class."
This comment is otherwise beneath a reasoned response.