Posts in March, 2016

The Django code in this post comes from the official Django tutorial.

I've tried not to be disingenuous with the comparison, and I'm only going to compare like with like, so I can show code from both frameworks and let you draw your own conclusions. I'll cover the areas where they differ in another post.

Both Moya and Django use models to map databases on to familiar data structures. In the case of Moya, the mapping is done with SQLAlchemy. Django uses its own ORM.

Here's the models.py from the Django tutorial and a Moya version: continue reading…

Have you ever needed to test multiple user accounts in your web app?

If you need to check your permissions are working correctly, you might find you are constantly switching users by logging in and logging out. Or you could have an admin user signed in to Chrome, and another user signed in to Firefox. Either way, its a bit of a pain.

Here's a simpler solution; define multiple aliases to 127.0.0.1 in your /etc/hosts file. That way you can have one tab open at http://adminuser:8000/ and another open at http://user1:8000/ with two entirely different user accounts.

This assumes of course that you have a development server running locally, and that your app is configured to serve to these 'domains'.

For example (append this to /etc/hosts):

I spent an evening adding 'progressive' loading of the title images to my blog.

The title images for this blog are 3840 × 2160 and a hefty ~650K each. That's entirely intentional; as a photographer I wanted them to look as sharp as possible and take advantage of high pixel density screens.

The only downside of hi-res photos is that even with a good internet connection, you can still see the photo loading as the browser reads the JPEG. It's visually jarring and way too reminiscent of the web, circa 2000s.

A reasonable solution is to first download a smaller lower-resolution version, then load the full resolution image on top of that. So the user sees something relatively quickly, without the visual contrast of an image loading on a blank background. continue reading…

One of my goals for inthing.io was to make posting events realtime, in that events appear without a page refresh, and within a fraction of a second. And that largely seems to work.

Here's a quick screencast that shows it working:

I'll post about how it works in detail at some point, but the general gist is that there is a Tornado websocket server that inthing uses to broadcast information about updates. That server may be worth open-sourcing if there is enough interest. It could be useful for other projects, and its entirely independent.

There a few options, but I'm going to suggest using the inthing Python module.

First install inthing with PIP. You'll probably know if you need to use sudo or not:

Now fire up Python, and enter the following (copy and paste each line after the >>> prompt):

So far so good. We now have a Stream object called stream, with a corresponding live page on the web. We'll have a look at that later, but lets first do something interesting with it. Enter the following function (copy and paste all the text after the >>>):

If that pasted correctly, we should have a function that generates the Mandlebrot set in ASCII. Add it to the stream with the following:

Finally, to get the URL of the stream, do this:

If you visit that URL, you should see the Mandlebrot set you just generated. continue reading…