A few years ago, when choosing a Python Web-framework with which to build Showmedo, life was a little simpler. The big CMS frameworks like Zope and its relatively user-friendly little bro Plone were reasonably well established and there were a few up and coming lighter frameworks, which promised to take one closer to the Python. Standouts here were Turbogears and Django.
I tried using Plone, even bought the book, but found it very unwieldy. Like so many frameworks it was perfectly happy until one wanted to do something outside its workflow plan. Then things got icky, really icky. There was also far too much ‘magic’ going on, and far too little connection with the underlying Python. After this experience Turbogears was a breath of fresh air. I could build a web-site in Python and leverage all the efficiency and elegance of the language up close and personal. What could be sweeter? I enjoyed the experience so much I didn’t give Django a fair crack of the whip. Although back then it was in beta and had been postponing a 1.0 release for a very long time.
Well, time moves on, and if there has been anything as intense as a framework war, it’s fair to conclude that Django has taken the spoils. Which seems fair. It’s a fantastically managed project, the documentation seems top notch (very important point), and the community is huge, enthusiastic and growing. For a more flexible experience Pylons is threatening to steal some of Turbogears thunder, offering a more modular, ‘best of breed’ alternative to Django. One of the big advantages here is the possibility of using the superb and acknowledged king of Python ORM database libraries, SQLAlchemy.
Anyway, today I come to praise something at the other end of the spectrum and newish to the field, namely Bottle.py. An entire web-framework packaged in a single Python module, which seemed crazy when first I heard of it. Bottle bills itself as a micro-framework, fast, simple and lightweight. Having had a chance to play around with it, I can testify to this. It makes a superb development server, among other things. After previous battles with Zope/Plone it’s pretty incredible to get a server up and serving pages in a few lines of Python, with one imported module. Here’s the ‘Hello World’ example:
from bottle import route, run
return 'Hello %s!' % name
So, if you fancy getting away from the complexity of the bigger frameworks and getting back to basics, Bottle.py is a great option and an amazing achievement.
In the meantime, a little selection of Python web-framework videos from the Showmedo vaults:
Kevin Dangor’s Ultimate DVD Turbogears set. Note: Turbogears 2 has seen some impressive changes to the framework, but much of Kevin’s presentations is still applicable.
We don’t yet have a bottle.py screencast, but there will be one soon. In the meantime, the closest to low-level web-appery we have is John Montgomery’s introduction to Python Web Programming CGI . Note: this is a club series but the linked introductory video is free and gives an overview.
Big kudos to Dai for adding his 40th Scribus Video to Showmedo. Judging from the feedback and Google, these are by some way the most popular Scribus screencasts on the web and a great testimony to Dai’s teaching skills and the efficacy screencasting.
You can see Dai’s Scribus collection here, and sample his 40th video below:
June 8th, 2010 | Category: News | Comments are closed
I think pretty substantial inroads have been made with regard to these goals. Not perfect by any means, but there is now a reasonably solid base on which to build. I’ll be documenting the changes a bit in the next week or so, but hope the new site is a much nicer prospect.
First a little screenshot to show what the vanilla widget looks like, after a bit of customization that you can perform on the Twitter site (you’ll find the widgets hanging off the ‘Goodies’ link at the bottom of your facebook page:
The widget shown is ok but:
I don’t like the header area, with those two Showmedos and a rather redundant Showmedo logo (this is appearing on the Showmedo site).
I’d also like to remove the padding to left and right of the tweets to align the text with the rest of the column content.
The font-used is lucida-grand but I would prefer to use Verdana, the default font for my site.
The ‘twitter’ badge on the bottom left, which is probably most likely to be clicked, takes users to the twitter home page ‘http://twitter.com’ but I would like it to direct to my twitter page ‘http://twitter.com/ShowMeDo’.
First we remove the header by finding its css class, ‘twtr-hd’, and using jQuery to hide it. This is equivalent to setting its css ‘display’ attribute to ‘none’.
To change the padding and font-family just add some css to your style-file:
Note the use of the !important flags to override existing settings and escape the css cascade. Now twitter themselves, rather frustratingly, set the widget font-style using an !important tag. This means that in order to override it we need to prioritise our !important using the identity ‘contentRight’ of the right column in which the twitter widget appears.
Lastly, in order to direct users clicking the twitter badge to ShowMeDo’s twitter site we use a little jQuert to change the ‘href’ attribute of the twitter link:
All Pythonistas will recognize the % operator used to format strings:
print "%s world from %s!"%('hello', 'showmedo')
This is similar to its ‘C’ antecedent but, as you would expect and probably know, Python allows containers other than tuples (in the case containing the ‘hello’ and ’showmedo’ strings) to be used in the string formatting operation.
The use of a dictionary rather than tuple to format the string provides a very handy little templating mechanism, used recently in the addition of video embedding strings to the site.
We start with our text-file template, one long string with no pesky line-breaks:
Note the variables scattered through the string starting with %(width)d. We’ll be using these, together with a Python dictionary, to create our video-specific embedding string. The method that does the legwork is shown below. It is a member of the ‘Video’ class and in this context ’self’ refers to the video instance in question:
We open the ‘embed_video.txt’ file (using a little Python os magic to establish a relative directory path) and read its contents, storing them in the ‘embed_template’ string. We then use the ‘%’ operator to assign values to the special (of the form %(foo)x) variables in the template, using a Python dictionary.
Showmedo caught some flak from the recent Westhost (our main server-provider) troubles. Catch this pretty interesting thread for the gory details. We were actually pretty lucky as the main site remained standing (not the case for many others) but did manage to lose our cron daemon. As a result some restart scripts which check on the health of, among other things, the Python Turbogears process running the site died. Now this script isn’t needed too often <fingers crossed> but if the site falls down and we’re literally asleep then it avoids more than a minute or sos downtime. The crontab line in question looks like this:
This line tells cron to run cron_start_showmedo.py every minute and the python process will check the health of Showmedo’s main process, our memcache and mysql servers and a few other things and restart them if anything has fallen over. Having lost cron, I needed a quick hack to perform these duties and found some very cool advice here at Slashdot. For various reasons the recommended bash-scripts seemed to be dying on me, and lacking the time or will to try debugging on fairly foreign turf, I hacked a little Python substitute to do much the same:
1 import time
2 import subprocess
4 while 1:
5 subprocess.Popen(['/usr/local/bin/python2.5', '/home/showmedo/showmedo/cron_start_showmedo.py'])
Not pretty, but at five lines quick and succinct. The line 4 while loop sets things running in perpetuity, line 5 uses Python’s newish (and pretty powerful) subprocess module call the cron_start_showmedo.py module and line 6 takes a 60 second break before continuing the loop.
The script sat duty while I waited for our cron-daemon to return and I slept sounder in my bed as a result.
Having to wear my sysadmin hat a little too often this week. I can honestly state that at least one of the bugs wasn’t my fault. At the moment our cron-jobs (timed unix processes) are failing to engage. So when the site falls down, there’s no handy cron-restart. Should be easy to track down and lots of fun :0
This little script, appearing below Showmedo’s video-boxes should allow easy embedding of the videos in blogs, articles and the like. As ever, avoid the WYSIWYG editors or the pristine HTML is likely to be horribly mangled.
If everything goes according to plan, you should be seeing a Showmedo video below (The first part of Chris Perkins’ rather wonderful series on Agile Development Tools in Python):
In this tutorial screencast you will learn how to install pygame on Linux, Mac, and Windows. I explain what you can find on http://pygame.org/download.shtml and how to find out if pygame wasn’t installed correctly.
One of the nice things about the web and coding in general is the occasional little distraction thrown up in the space of toolsets, coding-practice and the like. Today those eddies in cyberspace have brought fonts to the fore, after a while in which I really hadn’t given it too much thought. But the fact is, for someone hacking code all day a good font, while maybe not as critical as a good editor (VIM btw), is important. In the same way as buying a good bed is important, you’ll have been staring at this font for a good few life years, when time comes to cash in your programming chips. So is worth some consideration.
I generally go for a good, reliable mono-space default which is less likely to go horribly wrong (VIM sometime gets confused with widths) and is cross-platform and easy. Andale Mono tends to do the job, without feeling particularly classy. Anyway, the Consolas font crops up a lot in various programming contexts, e.g. Slashdot, Reddit, Stackoverflow.
So my curiosity has finally been piqued and I’m going to give it a try. I certainly don’t have the balls to try a proportional font, even if readability is improved by 18% or so. Here is today’s Slashdot thread, that undoubtedly gets to the heart of the issue. And a nice post from Hive-Logic about the top ten programming fonts, which suggests I should give Inconsolata a go. Which I probably will. The accompanying Reddit thread, that undoubtedly gets to the heart of the issue.
I’ll buzz back with an opinion when I’ve had time to absorb the changes. Exciting times…
Getting Flowplayer up and running on the site was not quite as smooth as the nice web-site and clean API suggested. I’ll be documenting the whole gory process in the ‘Building a Website (with Python, jquery etc..)’ series, but in the meantime I’ll mention the hideous hackery needed to get over the final hurdle that is Internet Explorer 7.
Somewhere along the line I was looking to pass a web-address, as part of an html overlay, into the flash-player. This has the standard form:
Now if one tries to pass that string into a flashplayer, all hell will break loose. Some of those symbols are going to be interpreted as controls rather than nice, neutral characters. For this reason they need to be ‘escaped’, and Turbogears provides a nice little function “quote_plus”, via Python’s urllib, to do just this. The result is this rather scary looking string:
This string makes it through the flash-player in all the browsers we’re interested in at Showmedo and produces a clickable flash-overlay which, strangely enough, takes one through to the page in question. Er, except (and twas ever thus), IE7 (not 8 <sigh>), where, when all the escape-artistry has taken place, we find ourselves directed to this non-existent page:
Now I’m guessing that somewhere at the heart of this is the issue of absolute and relative web-addresses. The second ‘http://…’ should be interpreted as an absolute address, but those unicode escape-characters have confused the system, making it treat the URL as relative, thus adding another root-url.
If I had time to explore this horror I would probably spend it doing something else, but in these time-starved times I just did a little bit of playing around and added a hack-function which undoes the ‘:’ and ‘/’ escapes of “quote_plus”, producing this:
If this gets through, the soon to be awesomeness of this blog will be seeping through to twitter automatically. Or crashing something somewhere. Going to keep on writing to hit the 140 limit – and see what it does with that.
January 17th, 2010 | Category: News | Comments are closed
In this final segment, we highlight concepts important for new scientific Python programmers, especially those coming from a Matlab environment. We discuss effective use of Python dictionaries and the timeit profiling module. We also discuss the defaultdict object for quick and easy database creation useful for keeping track of data in multiple parameter runs.
How to instantiate the previously created class ( Foo ) is shown, using IDLE (the Integrated Development Environment , that comes with the standard CPython distribution). 2 referrers are created to understand that up until all referrers are deleted, the Python garbage collector may claim the associated instance (destruct it, deallocate it).
variables in Python are simple names associated to an instance (it could be a value). Examples are shown to enforce this concept.
Now let’s look at a native document – OpenOffice works with .odt files (OpenDocument Text). Normally these are zipped structures that contain xml and binary data – here we look at what’s inside an .odt file.
If you’re curious, you can poke inside your own .odt files using my instructions.
You’ll find more tips at at 3monkeyweb.
An early hurdle when switching to OpenOffice will be the question of whether you can still share documents with MS Office users. Of course you can! Here we edit a document in both MS Word 2003 and OpenOffice 3.1 Writer to show that all formatting is preserved.
In just over 2 minutes we run you through the installation of OpenOffice 3.1 on Windows XP. We also show you how you can download a version of OpenOffice for Windows, Mac or Linux in different languages.
Visit OpenOffice.org to get started.
In this series we help you move from Microsoft Word (2003 is our example) to OpenOffice 3.1. We show you how to use both Word and Office to edit the same document and how to do all the usual functions in OpenOffice, as well as where to go to get your questions answered.
Visit OpenOffice.org to get started, see the live download counter and read more background at WikiPedia.