RSS

Tag Archives: flask

In defense of dynamic languages

There are a good many truths and there are a better set of likelihoods. Given the current state of dynamic languages today they are less performance than static and functional languages, however, it is also true that dynamic languages are more productive than static and functional languages. (I am not talking about savants)

Don’t optimize your code at the first stage. First make it right, then (if necessary) make it fast (while keeping it right). –erlang programming rules

It is likely that regardless of the size of your project, the size or makeup of your team, or the breakthrough that you think the project represents… that your project is going to have average results at best. The Google’s, FaceBooks and Twitters of the world are extreme edge cases. As proof, look at the iPhone app store. There are over 600,000 apps and only a very small fraction of those apps have the following that Angry Birds does.

So before you go off in a corner reinventing the wheel in your favorite language consider this. WHat is going to be your return on investment? I cannot blame you for learning a new language or tool that would enhance your marketability or even just for hobby sake. But if your intent is to make some money and maybe a little independence they you really need to consider your ROI. And if you’re making money then rewriting your killer app in whatever killer fast programming language is available (and popular) will make make plenty of sense.

This is why I’m hot on python and python’s django, tornadoweb, flask; perl and perl’s mojolicious; ruby and ruby’s sinatra and rails; redis, sqlite, zeromq.

PS: While I’m not a fan of erlang, partly because of what it represents, I really like it’s Programming Rules and Conventions(PRC). By comparison python’s PEP-8 is amateurish. The PRC starts off with ideas like the one quoted above and giving you ideas on how best to approach the problem. This is like python’s PEP-20 but again it’s like signing your name with a crayon instead of a fountain pen.

 
Leave a comment

Posted by on 2012/05/03 in architecture, ProgLang

 

Tags: , , , , , , , , , , , ,

Your Next Web Application Framework?

Suppose you are the person who has to make the decision as to what language and framework your startup is going to use to deploy it’s application.  What would you choose? There are so many interesting and qualified frameworks that are already powering a good portion of the internet.

(You don’t have to know the language and framework but enough to argue what makes it idea.)

What would it be?

For example: I read an article several years ago that strongly recommended Erlang. At the time it was a great idea. The author suggested that using Erlang would attract smart people and keep the actual number of respondents to something manageable. Since then I implemented several applications that in hindsight: (a) impossible to attract new talent. (b) the more time that passes the more fragile the app gets because my detail recollection is fading (c) and it lacks common tools that would make allow generalized apps to give access to “operators” instead of programmers.

Other examples include: perl, ruby, python… mojolicious, rails and sinatra, flask, tornadoweb, and django.

I have my ideas… what are yours?

 
Leave a comment

Posted by on 2012/04/12 in architecture, future, web

 

Tags: , , , , , , , , , , ,

What is so interesting about the Flask microframework?

I get that Flask has a lot of the same design patterns that Ruby’s Sinatra has. I suppose if one used a metadata approach to application construction/deployment that you might be able to basically interchange between them.

I did a search hoping to find out the differences between Flask and Tornado. I was rewarded with a page from the the Flask development doc. The contributor was suggesting that one might link or cascade Flask with either Tornado, Gevent, Gunicorn or some other proxy setup.

While mentioning Tornado the contributor says…

Tornado is an open source version of the scalable, non-blocking web server and tools that power FriendFeed. Because it is non-blocking and uses epoll, it can handle thousands of simultaneous standing connections, which means it is ideal for real-time web services. Integrating this service with Flask is a trivial task:

In the deployment section of the doc Flask makes is clear that the built-in webserver is strictly for development. The reasons are probably very similar to Rails’ webrick but in the case of Flask there are no explanations. Nor is there a recommendation just a list of servers.

I recently deployed a Tornado-ZeroMQ bridge in order to increase the transaction throughput. Sitting in front of the Tornado instance is a traditional webserver like apache, lighttpd, nginx. These webservers are serving static content because that is what they do best and the dynamic requests are passed thru. But why would I deploy lighttpd->tornadoweb->flask? There is plenty of room for improvement here but someone transitioning from sinatra to/from flask could be rewarded.

hello world from their respective websites:
(Flask)

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!" 
end
 
Leave a comment

Posted by on 2012/03/22 in Tools, web

 

Tags: , , , , , , , , , ,

Flask, Pystache and Bootstrap

[Update 2012-05-14] RDO pointed out that there were some changes in pystache. The first and foremost is that they changed the API structure entirely. While I might not have actually implemented that best practices as there are a number of ways to accomplish the same thing. I was more interested in staying try to the initial project structure.  So if you read the pystache Renderer class you’ll probably see everything you need. And to round the changes out I found a few bugs in the MANIFEST.in and __init__.py that have been corrected here. This code runs although it’s not loading any of the images and some of the css… as I have not updated the html to work the way it should. But it’s easy enough for the reader now that the rest is working. (One final note, edit the fluid.html file and change ../assets/ to /static/) 

I’m using flask, pystache and bootstrap in order to build a fast prototype. I’ve already written a little about this but the text was getting long in the tooth so I cut it off short of a complete description. I decided to break the work into a separate text… as it’s more manageable. Some of these instructions will vary if you are using virtualenv.

- download bootstrap

cd ${HOME}
mkdir -p git
cd ${HOME}/git
git clone https://github.com/twitter/bootstrap.git

- download the latest pystatche (pip does not have the latest code so go to github for it)

cd ${HOME}
mkdir -p git
cd ${HOME}/git
git clone https://github.com/defunkt/pystache.git
cd pystache
sudo python ./setup.py install

- download and install flask

sudo pip install flask

- download and install ‘modern-package-template’

sudo pip install modern-package-template

- create your project (answer the questions as best as you can)

cd ${HOME}
mkdir -p hg
cd ${HOME}/hg
paster create -t modern_package helloworld

- now you need to do a number of things in order to get pystache to work with bootstrap. I’m going to list them out and give examples where I can.

  • copy the ‘examples’ directory from bootstrap to the ‘src/helloworld’ directory
  • create a ‘static’ directory in the ’src/helloworld’ directory
  • copy the css and JS directory from the bootstrap directory to the static folder
  • update the MANIFEST.in file to include the examples and static directories
  • update src/helloworld/__init__.py
  • update src/helloworld/hello.py

FILE: src/helloworld/__init__.py

from helloworld import hello
def main():
    hello.main()

FILE: src/helloworld/hello.py

from pystache.loader import Loader
from pystache.renderer import Renderer
from flask import Flask, url_for

app = Flask(__name__)
@app.route("/simple")
def simple():
    loader = Loader(extension='html', search_dirs=[app.root_path+'/examples',])
    template = loader.load_name('fluid')
    renderer = Renderer()
    return renderer.render(template, {'person': 'Mom'})

def main():
    app.debug = True
    app.run()

FILE: MANIFEST.in

include README.rst
include NEWS.txt
recursive-include src/helloworld/examples *
recursive-include src/helloworld/static *

pystache is able to locate the templates stored in the source because I added the code ‘app.root_path’ to the template directory parameter in the pystache loader.

Flask is able to locate the static files because flask defaults ‘/static’ to the folder in the same directory as the current module. This did not require any special config. If you decided to move the file you would have to add some params to app.run()

Finally, since setuptools does not really recognize this project structure you have yo update the MANIFEST file yourself in order to tell setuptools to package the example/templates and static files.

One parting note. Jinga2 is installed with flask as part of the dependencies. I would have used Jinga2 but except for 2 reasons. 1) I’m experimenting with different languages and I’d rather use one template assembler. 2) There has been some recent criticism of template assemblers in general. The complaint is that they break the MVC model. It is currently impossible for Mustache to do that with the current syntax.

Serving any object like JS or PNG files need to be in the static directory or they need to be served by a web gateway in front of the flask instance. Like nginx, lighted or Apache.

I hope this article is better than the previous. I might clean that one up.

 
11 Comments

Posted by on 2012/01/18 in web

 

Tags: , , , , ,

Bootstrapping your next project with Bootstrap.

Bootstrap is a nice little web app starter framework released by the kind folks at Twitter. I’m not sure why they did it but I suppose that does not matter much. It’s nice, open, and fun.

For the purpose of this bootstrap project, which I’m calling freestrap, I’m going to select some technology that I previously installed in a recent article. The stack will be:

  • python
  • tornadoweb or maybe flask
  • beanstalkd
  • bootstrap and it’s deps
  • modern-package-template
  • mustache

I was going to implement a database layer too but I think that will be postponed until the next project is fully realized.

  1. At this point everything is already installed.
  2. You need to navigate to your project directory.  I like to create a git or hg folder immediately in my home.
    1. cd ${HOME}
    2. mkdir -p hg
  3. then you’ll need to create the project folder (‘freestrap’) with the modern packager.
    1. cd ${HOME}/hg
    2. paster create -t modern_package freestrap
  4. And you can run the application by typing
    freestrap
  5. if you execute the ‘tree’ command you should see something like this.
(currentenv)rbucker@soldev:~/hg/flafreeit/freestrap$ tree
.
├── bootstrap.py
├── buildout.cfg
├── HACKING.txt
├── MANIFEST.in
├── NEWS.txt
├── README.rst
├── setup.py
└── src
 ├── freestrap
 │   └── __init__.py
 └── freestrap.egg-info
 ├── dependency_links.txt
 ├── entry_points.txt
 ├── not-zip-safe
 ├── PKG-INFO
 ├── SOURCES.txt
 └── top_level.txt

3 directories, 14 files

That’s it for the bulk installation. Now I’ll integrate flask, mustache and bootstrap.

  • create src/freestrap/hello.py with the following code.
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
  return "Hello World!"
def main():
  app.run()
  • update src/freestrap/__init__.py to look like this
# Example package with a console entry point
from freestrap import hello
def main():
  hello.main()

now install the package and rerun the app:

  • python ./setup.py install
  • freestrap

You’ll see that the web server is running on 127.0.0.1:5000. If you’re like, however, you won’t be able to load the test page because it’s a remote server that needs to connect to 0.0.0.0. So change the last line in hello.py to:

  • app.run(host=’0.0.0.0′, port=5000,)

and with the next restart you’ll be able to point your browser to this app. Of course you always tunnel.

ssh -L 5000:localhost:5000 rbucker@myhost.remote.com

and then put http://localhost:5000

 
Leave a comment

Posted by on 2012/01/18 in web

 

Tags: , ,

“Mojolicious” has lost it’s Mojo

I’ve recently encountered my second issue with Mojolicious; since the Mojo team had recently added some core developers and reinstated their ticket system via GitHub I decided to open a ticket.

The first issue I presented to the team was their use of “daemon” as a command line param to launch your webapp, however, it unexpectedly ran the app in the foreground. My understanding of “daemon” and the behavior of any number of apps is that “-d” or daemon-mode meant background execution.

The issue I am currently working on is the new 2.37 version. It’s the first build from the new core team and that was a good thing, however, it would not build on my system (OpenBSD 5.0). So I reported it. The response that I received directed me to the CPAN team who verified that it worked.

Less obvious was the notion that I needed to check my dependencies. So I suggested exactly that… to which I received a reply that a) we were off topic and b) that there are no dependencies.

Well, ok… we were a little off topic… but there is such a thing as customer service. And while the cpan install file may not actually set the dependencies… they are listed on the CPAN page. So why not add that to the install?

So I’m up to my eyeballs in this. I’ve spent the last few months (calendar time) trying to wrap my head around this thing called Mojolicious. I happen to like perl and the CPAN. But I’m also frustrated with my two encounters with the Mojolicious team. They are neither smart enough or pretty enough to warrant this sort of snarkey-ness,

In the last 24-hours I’ve spent some time looking at flask, Werkzeug, Cyclone, and of course I already use Django and TornadoWeb. I also like python. The dependencies are easier here too.

 
1 Comment

Posted by on 2011/12/11 in web

 

Tags: , , , ,

 
One Page Docs

Creating a library one page at a time.

One Page Bugs

Reducing the friction of writing and fixing bugs or features.

Follow

Get every new post delivered to your Inbox.

Join 223 other followers