Showing posts with label python-frameworks. Show all posts
Showing posts with label python-frameworks. Show all posts

Saturday, May 30, 2015

Moire with Spyre (a Python web framework)

By Vasudev Ram




The image above is of Uppsala Cathedral, Sweden, with its spires.

From the Spyre site:

Spyre is a Web Application Framework for providing a simple user interface for Python data projects. It is by Adam Hajari

I had come across Spyre recently via this post by Ian Oszvald:

Data Science Deployed – Opening Keynote for PyConSE 2015. More about Ian Oszvald here.

So I installed Spyre with the command:
pip install dataspyre
which in turn installed NumPy, pandas and matplotlib, all of which went through okay, though I saw some compiler / linker error messages scroll by as some compilation of C modules was happening (as part of the installation).

Then I tried it out a little, this time just using one of the simple applications on the Spyre site, without any modifications, like I sometimes do.
from spyre import server

import matplotlib.pyplot as plt
import numpy as np

class SimpleSineApp(server.App):
    title = "Simple Sine App"
    inputs = [{ "input_type":"text",
                "variable_name":"freq",
                "value":5,
                "action_id":"sine_wave_plot"}]

    outputs = [{"output_type":"plot",
                "output_id":"sine_wave_plot",
                "on_page_load":True }]

    def getPlot(self, params):
        f = float(params['freq'])
        x = np.arange(0,2*np.pi,np.pi/150)
        y = np.sin(f*x)
        fig = plt.figure()
        splt1 = fig.add_subplot(1,1,1)
        splt1.plot(x,y)
        return fig

app = SimpleSineApp()
app.launch()
As you can see, the structure / model of the code for a Spyre app does not seem to be very similar to that of, say, a Flask or a Bottle app. But then, there is no reason that it should be.
Anyway, I ran this app with the command:

python simple_sine_example.py

and then browsed to:

http://127.0.0.1:8080/

where I then gave different values for the input parameter freq, to see what would happen. I found that running it with certain values of freq created interesting Moire patterns in the output; hence the title of this post.

Here are a few screenshots below that show those moire patterns:

With freq = 764:


With freq = 472:


With freq = 475:


With freq = 479:


Notice that in some cases, such as with the values of 472, 475 and 479 for freq, the small changes between those values results in significant changes in the output image. Not only that: for 472 and 479, the images are similar (look sine-wave-y), but for 475 (which is between the other two values), the image looks almost like straight vertical lines. (It isn't, of course; it's still a sine wave, but more compressed along the horizontal axis.) I've not analyzed the reason for this in detail; something to do with the periodicity of the sine function, is my rough guess, but I have seen similar phenomena when drawing other kinds of computer graphics on the screen; those also involved trigonometric functions such as sine and cosine.

Spyre away :)

P.S. In other news, I was recently profiled in PyDev of the Week.

- Vasudev Ram - Online Python training and programming

Dancing Bison Enterprises

Signup to hear about new products or services that I create.

Posts about Python  Posts about xtopdf

Contact Page

Friday, December 12, 2014

Project Enferno: RAD framework with Python / Flask / MongoDB / Redis

By Vasudev Ram



Just saw this via Python Weekly's email newsletter. Enferno seems to be a RAD (Rapid Application Development) framework of sorts, using Python, Flask, some Flask extensions, MongoDB AND MongoEngine, and Redis.

Enferno framework

I've worked on a project or two using Python, Flask and MongoDB (which is a NoSQL database), and found it a useful combination for developing apps relatively quickly. Enferno may be worth checking out, which I will do sometime later and may then blog about it.

Here is an article about NoSQL by Martin Fowler that may be of interest to newcomers to the field. Fowler and Pramod Sadalage have also written a book, NoSQL Distilled, which is linked to from Fowler's NoSQL article page. I've read Fowler's earlier book UML Distilled, a small and useful summary of UML, and it looks from the title that their NoSQL book may be on the same lines.

- Vasudev Ram - Dancing Bison Enterprises

Signup for email about interesting new stuff from me.

Contact Page

Other Python posts | Posts about xtopdf

Sunday, November 17, 2013

Book review: Instant Flask Web Development

By Vasudev Ram



Flask is a Python micro-framework that is fairly popular.
I had first blogged about it a couple of years ago, here:

Flask, new Python microframework

Recently, I had been working on a commercial Flask project for some time, when Packt Publishing asked me if I would review one of their books, Instant Flask Web Development. I did it. The review is below.

Review of book "Instant Flask Web Development", author Ron DuPlain, publisher Packt Publishing:

The book seems to be meant for people who already have some experience with Python.
Some parts of it that cover using Twitter Bootstrap and CSS in Flask templates, will also need knowledge of those topics.

Starts with a simple Hello World Flask app and explains some of the concepts involved.

Some of the topics covered in the book are:
- making a simple Flask app
- mapping URLs to functions (a.k.a. routing)
- HTTP request and response handling
- using databases, static file handling, and form and file uploads
- database CRUD (Create / Read / Update / Delete) operations
- sessions and authentication
- error handling
- deploying Flask apps using nginx and gunicorn

Uses Flask-Script to create command line tools to manage the Flask apps created.

Flask-Script is a Flask extension that provides support for writing external scripts in Flask.

(Flask has an facility for writing extensions that add to the functionality of the base Flask package, and there are multiple such extensions available.)

The book then starts on a scheduling application, which is used as a tutorial to illustrate various Flask features. This app allows the user to Create, Read, Update, Delete (i.e. CRUD) and List appointment records.

It shows how the use the route decorator of the Flask app object, to map various URLs that the app supports, to functions of the app that implement the actions specified by those URLs.

I noticed what seems to be an error in this section; in the middle of the code that maps the URLs to functions, there is this line:
@app.route(...) and def appointment_edit(...).
which is probably an inadvertent copy/paste from some of the text. But it would make the code fail to run, if copied as is from the ebook.

They do specify URLs (on the Packt site) from where you can download the source code for the program examples in book, though.

The use of the Flask url_for() function is described.

There appears to be an error in this section of code:
@app.route('/appointments/<int:appointment_id>/')
def appointment_detail(appointment_id):
    edit_url = url_for('appointment_edit',
    appointment_id=appointment_id)
    # Return the URL string just for demonstration.
    return edit_url
The function is called appointment_detail, but the url_for function is passed an argument 'appointment_edit'. In this case it would work, because it is just returning the URL string for display, not actually doing the appointment detail display.

Then it moves on to talk about how to handle different HTTP methods in Flask, as defined by the HTTP protocol, including GET, POST, etc.

It also mentions that instead of using the Flask route decorator, you can use a method, app.add_url_rule, as an alternative.

The section on using Jinja templates requires knowledge of CSS and JavaScript, and also Twitter Bootstrap and jQuery. The book gets a bit into Jinja features like macros.

Simple, Intermediate and Advanced sections are interspersed through the book (except for the first few sections, which are all Simple).

The book shows how to use some of Flask's error handling techniques, including how to generate a custom error page.

It ends with describing how to deploy a Flask app to production using nginx and gunicorn.

Other posts about Flask on this blog.

Other posts about Python on this blog.

- Vasudev Ram - Consulting and training on Python, Linux, open source




O'Reilly 50% Ebook Deal of the Day


Thursday, September 27, 2012

Waitress, a pure Python WSGI web server

By Vasudev Ram


Waitress is a pure-Python WSGI server. (It is part of the Pylons Project.) From the Waitress site:

[ Waitress is meant to be a production-quality pure-Python WSGI server with very acceptable performance. It has no dependencies except ones which live in the Python standard library. It runs on CPython on Unix and Windows under Python 2.6+ and Python 3.2.
...
It supports HTTP/1.0 and HTTP/1.1.
]

Usage example:
from waitress import serve
serve(wsgiapp, host='0.0.0.0', port=8080)
or
from waitress import serve
serve(wsgiapp)
The second example serves the application on all IP addresses, on port 8080.

You can download Waitress from the Python Package Index (PyPI).

- Vasudev Ram - Dancing Bison Enterprises


Tuesday, July 24, 2012

Aspen, Python framework that uses simplates and the file system

By Vasudev Ram


Aspen, a different kind of Python framework

It makes some interesting use of the file system which I have not seen in other Python frameworks. Also, there are several testimonials about it by users, on its site. It's one of the smaller frameworks, not a large one like Django.

- Vasudev Ram - Dancing Bison Enterprises