Showing posts with label flask-script. Show all posts
Showing posts with label flask-script. Show all posts

Tuesday, 21 June 2011

Flask-Coffee - Fill your flask with coffee

Flask-Coffee is a Flask extension that compiles .coffee CoffeeScript files into .js JavaScript files for you if they have changed before your app renders.

This is a nice idea in development and not so nice an idea in production, so please keep that in mind.

Prerequisites

You will need to have to have Node.js installed with NPM and coffee-script.

You can follow the instructions in my previous article to install Node.js and then it's just

npm -g install coffee-script

Installation

Install Flask-Coffee with pip:

pip install flask-coffee

Usage

from flaskext.coffee import coffee

coffee(app) 

This will watch your app’s static media directory and automatically render .coffee files into .js files in the same (sub)directory.

The best way to incorporate Flask-Coffee into your development is as described for flask-lesscss in my earlier flask-script tutorial.

Contribute

If you want to contribute email me at the email at http://pypi.python.org/pypi/Flask-Coffee after checking out the code at http://bettercodes.org/projects/flask-coffee.

Thursday, 9 June 2011

Let Your Imagination Run Riot On Our Flask-Script Example

In my last article in this series, "Adding Shell Access to Our Flask-Script Example" I showed you how you could indeed get shell access from our script.

You see the whole point of Flask-Script is that it gives you scripted access not to the application, but to the environment in which the app runs. You don't actually need to be running the app, or importantly, to interfere with the app to do some task or other.


An Example
For example, lets say you want to load some data into the database model.

There are three ways you could do this:

  1. Write a script that understands your app's data model and get it to insert the data. You may be able to import the model module straight from your app, but you may not, due to dependencies etc.
  2. Run the code straight from your app, possibly using an admin screen somewhere. This means designing and implementing the screens - including the obvious security requirements.
  3. Use Flask-Script like this:

from application.model import Articles
import csv


@manager.command
def ingest_csv():
    '''read data from a csv file and ingest it into the database'''
    reader = csv.reader(open('articles.csv'))
    for row in reader:
        a = Article()
        a.init_from_csv_row(row)


and then;

python manager.py ingest_csv

Simpler I think you'll agree?



Scheduling Task
There is one other huge advantage to using Flask-Script; you can run them from crontabs.





*/10 * * * * /some/path/bin/python /some/path/manager.py ingest_csv >>/tmp/ingest.log 2>&1

Other Ideas
Here are some other examples of things you might want to dovia script:
  • Purge old data from your database.
  • Backup data from your database.
  • Export data from your database.
  • Do anything else you want from your database!
  • Flush your caches.
  • Mail out reports.
  • Other stuff I haven't thought of. Let me know!
Okay, enough of all this, go and enjoy!


    Tuesday, 7 June 2011

    Adding Shell Access to Our Flask-Script Example

    The starting example of how you can use flask-script was given in my earlier article;
    A Simple Flask-Script Example.

    Next thing we are going to add is the ability to run a shell which that shares your applications environment. You need to decide what you ant to import and put it in the shell_context function. Call it whatever you want, but it has to return a dict with your context items inside. To keep it simple I just import the app itself. The lines you need to change are:

    from flaskext.script import Manager, Server, Shell
    

    and

    def shell_context():
        return dict(app=app)            
                
    if __name__ == "__main__":
        manager.add_command('dev', DevServer())
        manager.add_command('test', Test())
        manager.add_command('zen', ZenTest())
        manager.add_command('shell', Shell(make_context=shell_context))
        manager.run()
    

    now test it:

    python manager.py shell
    
    >>> app.debug
    True
    >>> app.logger
    <flask.logging.DebugLogger instance at 0x2ee3518>
    

    and so on.

    There's one final installment to follow - Let Your Imagination Run Riot On Our Flask-Script Example


    The full listing for what we did here is below:

    from flaskext.script import Manager, Server, Shell
    from flaskext.zen import Test, ZenTest
    import application
    
    
    app = application.create_app()
    
    manager = Manager(app)
    
    class DevServer(Server):
        
        def handle(self, app, host, port, use_debugger, use_reloader):
            try:
                from flaskext.lesscss import lesscss
                lesscss(app)
            except: pass
    
            app.run(host=host,
                port=port,
                debug=use_debugger,
                use_debugger=use_debugger,
                use_reloader=use_reloader,
                **self.server_options)
        
    def shell_context():
        return dict(app=app)            
                
    if __name__ == "__main__":
        manager.add_command('dev', DevServer())
        manager.add_command('test', Test())
        manager.add_command('zen', ZenTest())
        manager.add_command('shell', Shell(make_context=shell_context))
        manager.run()
    

    A Simple Flask-Script Example

    Here is a simple flask-script example that you can use to start your application in development mode, or test modes.

    I know that you can do that without flask-script, but the point of this script is that you can do so much more from this starting point. Notice for example that I'm trying to load flask-lesscss and execute it (which won't work, but won't fail if you don't have lesscss installed.)

    To get the application (in the 'application' module) running you just need to do one of the following:

    python manager.py dev
    python manager.py test
    python manager.py zen
    

    I show how to add shell access in the second article in this series. Meanwhile why not let us see what you have in your manager script?

    from flaskext.script import Manager, Server
    from flaskext.zen import Test, ZenTest
    import application
    
    
    app = application.create_app()
    
    manager = Manager(app)
    
    class DevServer(Server):
        
        def handle(self, app, host, port, use_debugger, use_reloader):
            try:
                from flaskext.lesscss import lesscss
                lesscss(app)
            except: pass
    
            app.run(host=host,
                port=port,
                debug=use_debugger,
                use_debugger=use_debugger,
                use_reloader=use_reloader,
                **self.server_options)
                
                
    if __name__ == "__main__":
        manager.add_command("dev", DevServer())
        manager.add_command('test', Test())
        manager.add_command('zen', ZenTest())
        manager.run()