Monday, November 28, 2011

Web2py Caching and Google App Engine

With the recent billing changes to Google App Engine, I was presented with a practical and monetary incentive to optimize my web2py app running on GAE.

At the suggestion of Jason Brower, I decided to look into implementing caching.  As a result, I found that this is one amazingly simple way of reducing costs and improving performance of my web2py app.

While there may be other ways of going about this that appear to be more involved, implementing caching can be as simple as adding the following decorator at the start of a function within your controller:

@cache(request.env.path_info, time_expire=3, cache_model=cache.ram)


You can read more about putting this into practice in Chapter 4 of the web2py book.

Here is a visual representation of the results:


Here, the blue line represents dynamic pages being served and the orange line represents pages served out of cache.

Enabling caching resulted in faster page loads for users, reduced costs, less stress on Google App Engine and a dramatic drop in errors messages (bringing them to effectually zero over a 12 hour period).

Where you are really going to gain performance with this are the functions which contain costly database queries.  That said, there are also some issues that you will need to look out for.

After enabling the caching decorator, you will want to test all your views using GAE's dev_appserver.  To catch errors and obtain a traceback, I found it useful to start GAE's dev_appserver.py with the -d command line argument to enable debugging.

While testing the app within web2py's built-in web server natively produced no errors, when testing the application using GAE's dev appserver, I found a few instances where certain views returned an error.  In particular, I found myself dealing occassionally with a PicklingError.  Fortunately, the great folks in the web2py group (anthony, howesc, massimo and Bruno Rocha) really came through, as they often do, with some great advice on resolving this issue.

In particular, rather than returning the dictionary (using return dict() ) like we usually do, using response.render(dict()) helped in many instances:

return response.render(dict(yourobjectname=yourdictionary)) 

In two other instances, I found I had to use python to get around an orderby within a select:

pledges=db((db.pledge.segment==segment_id) & (db.pledge.read != True)).select(orderby=~db.pledge.created_on)


When I removed orderby=~db.pledge.created_on from the query, the error went away,

however, we really don't want to loose the ability to order our query and serve the most recent results first.  So, to achieve the same results, we now needed to enlist python:


    # pledges=db((db.pledge.segment==segment_id) & (db.pledge.read != True)).select(orderby=~db.pledge.created_on)
    # orderby=~db.pledge.created_on seems to cause an error in GAE when cache is turned on, therefore:
    pledges=db((db.pledge.segment==segment_id) & (db.pledge.read != True)).select()
    sorted_pledges = []
    for row in pledges.sort(lambda row: row.created_on,reverse=True):
        sorted_pledges.append(row)
    return response.render(dict(pledges=sorted_pledges))


Viola!  We now have arrived at the place we need to be, plus we have caching.

After testing this in GAE's dev_appserver checking for any errors (and presumably rebuilding any indexes that need to be rebuilt) you are now ready to simply deploy your web2py app on GAE with caching.


------------

That said, I still have a question about what the difference is exactly between return dict() and and return response.render(dict()), but I'm really glad about the results.

Saturday, September 3, 2011

Re-evaluating my relationship with public media

I’ve finished my work with the amazing Knight-Mozilla Ideas Challenge and have been thinking about how things went. While there was an amazing amount of ideas around innovation, some of the advice that I took to heart late in the program was not aimed at fostering pure innovation as it was aimed at developing and strengthening an established idea or business; in particular, I had focused on the ideas of:

  • iterating, constantly updating your product, based on feedback 
  • and of listening, deeply to the goals of your audience (as suggested by Storify's Bert Herman)

With this in mind, there was one comment from Patrick which I had downplayed early on as I had been developing the Public Radio Roadtrip, but here it is, and it had still remained unforgotten:
It would we sweet if we could put [the public radio roadtrip] on our sites as a widget and define a certain starting area, such as our service area or state. I suppose the stories pulled would have to go back far enough to populate the less written-about regions.
Listening to this request had me re-thinking my approach and deciding that what I needed to build was an import/sync tool to update stories from NPR and then creating an automatically updateable, easily embeddable map that stations could use to show stories from NPR based on coverage area.

I was assuming, in the absence of significant use and regular feedback on the Public Radio Roadtrip in-which I could base frequent iterations of the product, that this one comment was indicative of a larger segment of my target audience.

Additional benefits of building an import/sync tool is that I could build upon KCRW’s NPR API module purely using python and contribute back to the project and possibly help other developers as well.

Yet, part of me thinks that all this is just me spinning my wheels.

And yet another part of me hopes that my work has at least added something relevant the larger conversation about public media, or even specifically around discussions regarding location-based audio. I wonder, for instance, if my post on Navigating Public Media Stories via GPS might well have been a footnote in the discussion that Zach Brand and others at NPR have likely been having leading up to their proposal for a presentation at SXSW entitled Auto Meets Mobile: Building In-Vehicle Apps.

While another part of me thinks I should refocus my efforts altogether.

Let me explain. Recently, in creating my Final Project for the Knight-Mozilla Ideas Challenge, I had a great experience with using the popcorn.js javascript library for syncronizing audio and images.  I found the process to be exceptionally fun. Here is a link to the presentation http://johntynan.com/presentations/nearbynews/finalpitch/ (Please note that, at the moment, this only runs in Firefox 4 and Google Chrome).  In working on this, I was reminded about how voicing for radio is about writing!

But I also was inspired about using this JavaScript library in other ways. Just like I had used proprietary animation tools to create an animation for an interview that Rene had done with me, I can see how this particular tool could be used to present existing radio stories visually, like what the Rauch Brothers are doing with StoryCorp, or Sarah Vowell’s collaborations with an artist friend using post-it notes (I wish I could find a link to these, they’re brilliant and inspiring), or the playful animations for KUT's O'Dark 30, or the excellent and inspiring New York Times' One in 8 Million project. I’m sure there are other examples that could be found in public media, and if you have them, I’d be glad to hear your suggestions.

So, I feel like I’m at the crossroads for my public media work.

I have found over the last year, that being an independent web developer with an interest in public media presents some interesting challenges.  While twitter, direct email and other avenues of communication give some glimpses of the goings-on of the industry, I wonder if  by not working at a station, or in coordination with a station, removes the independent developer from an important part of the dialog and presents a distinct distance from people who actually who would benefit from your work.  Supporting this idea, I was heartened by a recent post at NPR Digital's Core Publisher blog outlining the Program Director at Michigan Radio Tamar Charney's experience in Transforming our Web Strategy as one of organizational change.  Following twitter updates or following tumblr blogs give brief hints at larger changes, but I am pretty sure they are no substitute for being an integral part of the conversation happening at the station/national level.

And that's why I think it's pretty advantageous that the good folks at AIR and the CPB are sparking up a new new MQ2-inspired initiative “designed to pair inventive producers with local stations to increase the capacity for innovation in public media.”  I believe I can see the benefits that such a program would present for stations and for creative producers and developers.

But perhaps there are additional ways for developing my interests and talents within public media.  Perhaps joining AIR and pitching stories to news outlets or picking up tape syncs (especially while we're in Paris) might be a good direction to take.

Given that my family and I are projected to spend five years in China starting in 2013, I wonder if there are ways where this could be turned into an opportunity for both my friends and colleagues in public media, for their audiences and for myself?

Sunday, August 7, 2011

NearbyNews - Mozilla NewsLab Final Project

The Pitch



Design document


High-level architecture of NearbyNews components and processes

NearbyNews requires almost no installation.

NearbyNews leverages the newsroom’s existing infrastructure in that stories are fed into the NearbyNews database by way of the news organization’s RSS feed.

News organizations are also required to place one simple block of JavaScript into their page template and NearbyNews maps for all of their stories will automatically appear on the appropriate article pages.

Settings can also be configured to have newly published stories mapped automatically.  After an initial import of stories into the NearbyNews database, organizations may also opt to manually add new stories by hand.


(The interface for the NearbyNews story tool)

To review and edit maps, reporters and editors would login to a web-based dashboard.  A browser-based web app will be available guide news staff through the complete geolocation process using the story tool.  The can also map multiple stories within a series using the series tool.

Detailed documentation and on-site training will be available, as well as remote support by telephone and on-line chat.


NearbyNews is built collaboratively with reporters and editors


NearbyNews will be built for the newsroom, in the newsroom.  And understanding the newsroom culture of any participating organization will be part of the project.  Getting to know journalists and news directors and listening to them discuss their views on mapping is an important step in the process.  Journalists and editors may be asked to talk out loud while they use the NearbyNews tools and notes could be made regarding any difficulties or additional functionality that is requested.   Reporters could also be videotaped using the tool (with care to recognize excessive hand movements).  Adjustments to the interface could be made iteratively based on feedback.

NearbyNews will be built collaboratively with newsroom and open-source developers.  This code will be fully open-sourced.  A special fork of the program will be made for each participating news organization.  Enhancements made by newsroom or open-source developers can be merged back into the base product for the benefit of all.

Building an active, happy developer community depends on writing highly usable, high-performing, quality code along with quality documentation that anticipates the developers needs at every step and reduces as many barriers to adoption as possible.


NearbyNews leverages the following apps and platforms

A prototype of the NearbyNews can be found at publicradioroadtrip.appspot.com and the code can be found  on Google Code.  NearbyNews is currently built using Python and Web2py and can easily and cost-effectively be deployed on Google App Engine.  It uses GoogleMaps and jquery and KCRW’s NPRAPI Python library.  Future enhancements draw inspiration from Guilhem Vellut’s tutorial on Text Geotagging with Open Calais and Chris Heilmann’s GeoMaker and more.

NearbyNews aims to explore and resolve the following issues and challenges

The process of extracting location information from news stories based solely on a description or transcript (using Yahoo Placemaker and OpenCalais) is not entirely accurate.  Refining geographical information by a reporter or other staff member may be preferred.

The question of what to do with stories that are "not mappable” is a good one. The question of what to do with stories which, by their subject matter, are more general or conceptual should be considered in more detail.

The benefits of integrating geolocated stories into location-based services like Layar or Google Latitude still needs to be explored.  What will be the benefits to the news organization in terms of strategy, time?  Best practices around this strategy should be published.

How to best use location information to increase revenue?  How effective is advertising targeted to a neighborhood or region.  Would this really add value to advertisers?

How to create a user experience in the physical world around news?  Best practices for organizing, for example, a “walking tour” based on a particular topic or series of stories have not been established.  Would this kind of community engagement increase audience?  These experiments need to be tried and refined and the results published.



Business Brief

Mapping stories is an important part of framing a story and helping listeners relate to your reporting and to their world, yet this is a feature that is noticeably absent from many news stories online.  NearbyNews provides a news mapping service as well as an easy to use set of tools that are tailored specifically for journalists.  Rather than focusing on complex data visualizations, NearbyNews aims to provide a fast, practical way to geolocate the stories coming out of your newsroom.  NearbyNews works equally well as a way to map collections of stories in a series; such as your newsroom's coverage of seasonal wildfires.

There are a handful of mapping tools on the market, but none are tailored specifically for news organizations.  Some tools are more general like Google Maps, some are specific to disaster relief like Ushahidi’s CrowdMap, some cater more towards data visualization like TileMill.  Other tools focus more on crowdsourcing like broadcastr.com.  However, NearbyNews is the only mapping tool made specifically for journalists where speed, simplicity and ease of use is a premium -- and NearbyNews comes at a price-point that any newsroom can afford.

NearbyNews easily integrates with your existing tools and workflow, places little demand on your reporting staff and yet can be used to create in-depth maps as the story or series requires.

NearbyNews also provides many additional benefits and opportunities.  Your audience will benefit by discovering your stories serendipitously via their mobile phone (using a familiar Google Maps interface) in the places that are relevant to the stories and relevant to them.  Your sales departments and your local business partners will benefit from using NearbyNews in that advertising can be targeted to specific neighborhoods or regions.  Your organization may enjoy increased visibility and perceived value in your community by having your stories positioned in the physical world as location-based services become more widespread.