Category Archives: Web Framework

A Python web framework that should work well across browsers

Flask 101: Filtering Searches and Deleting Data

Last time we got our Flask based music database application partially functional. It could now add data to the database, edit said data and also display everything in the database. But we didn’t cover how to filter the data by using the user’s filter choice (Artist, Album name or publisher name) and search string. We also didn’t cover how to delete items from the database. That is the two-fold goal of this article.


Filtering Search Results

Filtering search results using SQLAlchemy (via Flask-SQLAlchemy) is actually quite easy. All you need to do is create some very simple query objects. Open up the main.py file that we were editing last time and replace the search_results() function with the following version of the code: Continue reading Flask 101: Filtering Searches and Deleting Data

Flask 101: Adding, Editing and Displaying Data

Last time we learned how to add a search form to our music database application. Of course, we still haven’t added any data to our database, so the search form doesn’t actually do much of anything except tell us that it didn’t find anything. In this tutorial we will learn how to actually add data, display search results and edit entries in the database.

Let’s get started!


Adding Data to the Database

Let’s start by coding up our new album form. Open up the “forms.py” file we created in the last tutorial and add the following class:

class AlbumForm(Form):
    media_types = [('Digital', 'Digital'),
                   ('CD', 'CD'),
                   ('Cassette Tape', 'Cassette Tape')
                   ]
    artist = StringField('Artist')
    title = StringField('Title')
    release_date = StringField('Release Date')
    publisher = StringField('Publisher')
    media_type = SelectField('Media', choices=media_types)

This defines all the fields we need to create a new Album. Now we need to open “main.py” and add a function to handle what happens when we want to create the new album. Continue reading Flask 101: Adding, Editing and Displaying Data

Flask 101: How to Add a Search Form

In our last article, we added a database to our Flask web application, but didn’t have a way to add anything to our database. We also didn’t have a way to view anything, so basically we ended up having a pretty useless web application. This article will take the time to teach you how to do the following:

  • Create a form to add data to our database
  • Use a form to edit data in our database
  • Create some kind of view of what’s in the database

Adding forms to Flask is pretty easy too, once you figure out what extension to install. I had heard good things about WTForms so I will be using that in this tutorial. To install WTForms you will need to install Flask-WTF. Installing Flask-WTF is pretty easy; just open up your terminal and activate the virtual environment we set up in our first tutorial. Then run the following command using pip:

pip install Flask-WTF

This will install WTForms and Flask-WTF (along with any dependencies) to your web app’s virtual environment. Continue reading Flask 101: How to Add a Search Form

Flask 101: Adding a Database

Last time we learned how to get Flask set up. In this article we will learn how to add a database to our music data website. As you might recall, Flask is a micro-web-framework. That means it doesn’t come with an Object Relational Mapper (ORM) like Django does. If you want to add database interactivity, then you need to add it yourself or install an extension. I personally like SQLAlchemy, so I thought it was nice that there is a ready-made extension for adding SQLAlchemy to Flask called Flask-SQLAlchemy.

To install Flask-SQLAlchemy, you just need to use pip. Make sure that you are in your activated virtual environment that we created in the first part of this series before you run the following or you’ll end up installing the extension to your base Python instead of your virtual environment:

pip install flask-sqlalchemy

Now that we have the Flask-SQLAlchemy installed along with its dependencies, we can get started creating a database! Continue reading Flask 101: Adding a Database

Flask 101: Getting Started

The Flask 101 series is my attempt at learning the Flask microframework for Python. For those who haven’t heard of it, Flask is micro web framework for creating web applications in Python. According to their website, Flask is based on Werkzeug, Jinja 2 and good intentions. For this series of articles, I wanted to create a web application that would do something useful without being too complicated. So for my learning sanity, I decided to create a simple web application that I can use to store information about my music library.

Over the course of multiple articles, you will see how this journey unfolded.


Getting Setup

To get started using Flask, you will need to install it. We will create a virtual environment for this series of tutorials as there will be a number of other Flask dependencies that we will need to add and most people probably don’t want to pollute their main Python installation with a lot of cruft they may not end up using. So before we install Flask, let’s create a virtual environment using virtualenv. If you want to use virtualenv, then we will need to install that with pip: Continue reading Flask 101: Getting Started

Getting Started with pywebview

I stumbled across the pywebview project a couple of weeks ago. The pywebview package “is a lightweight cross-platform wrapper around a webview component that allows to display HTML content in its own native GUI window.” It uses WebKit on OSX and Linux and Trident (MSHTML) on Windows, which is actually what wxPython’s webview widget also does. The idea behind pywebview is that it provides you the ability to load a website in a desktop application, kind of Electron.

While pywebview claims it “has no dependencies on an external GUI framework”, on Windows it requires pythonnet, PyWin32 and comtypes installed. OSX requires “pyobjc”, although that is included with the default Python installed in OSX. For Linux, it’s a bit more complicated. On GTK3 based systems you will need PyGObject whereas on Debian based systems, you’ll need to install PyGObject + gir1.2-webkit-3.0. Finally, you can also use PyQt 4 or 5.

You can use Python micro-web frameworks, such as Flask or bottle, with pywebview to create cool applications using HTML5 instead of Python.

To install pywebview itself, just use pip:


pip install pywebview

Once installed and assuming you also have the prerequisites, you can do something like this:

import webview
 
webview.create_window('My Web App', 'http://www.mousevspython.com')

This will load the specified URL in a window with the specified title (i.e. the first argument). Your new application should end up looking something like this:

Continue reading Getting Started with pywebview

Using pyGal Graphs in Flask

I recently took on a project where I needed to graph some data on a webpage using data I had queried from a database. Since I love Python, I decided to use it to accomplish this task. I went with Flask for serving the webpage and pygal for creating the graphs. In this tutorial, I will show you how to do that too, but without the database logic. Instead, we’ll get weather data from the Weather Underground and graph that. Let’s get started!

Continue reading Using pyGal Graphs in Flask

Real Python for Web Development Book on Kickstarter

Last week I was contacted about a cool sounding book project on Kickstarter: Real Python for Web Development, featuring web2py by Michael Herman. I have to admit that I’m not familiar with Mr. Herman or the person who originally contacted me about the book, but since I enjoy reading Python books and this one sounded interesting, I thought I’d let my readers know about it too. You can support the project yourself if you want. He’s recently added some tutorials for Flask to the book, so you can learn a little about two Python web frameworks!

TurboGears 2: Setting up on Windows

TurboGears is one of several web frameworks for Python that are available. The most popular by far is Django. Where I work, we chose TurboGears because of its integration with SQLAlchemy which supports composite keys. At that time, Django did not support that feature and I am not sure if it does yet. Anyway, I develop almost exclusively on a Windows box and have found the TurboGears’ documentation on the subject a little confusing. So here’s how I do it.

Note: We’ll be using TurboGears 2.1 in this tutorial Continue reading TurboGears 2: Setting up on Windows

Pyowa – July 2010 Wrapup

On Thursday, July 1st, we had our July Pyowa meeting. It was hosted by Matt Morrison at the IMT Group’s building in Des Moines, IA. We had our largest attendance ever with a total of 15 men showing up. Tavern Pizza and pop were served, which was also a first…we’ve had pop before, just not any food!

We had two presentations. The first was an around 70 minutes in length and covered introductory materials about Django, a full-stack web framework written in Python. It was given by our host and he also included anecdotes about how his company uses Django and what challenges that has presented him. Next up we had a quick talk about TurboGears, another web framework. TurboGears is actually a collection of various Python modules that have been pieced together, which makes it much more modular than Django. However, Django has a lot more users behind it and there are some definite advantages to having everything builtin. Anyway, the TurboGears presentation covered a group of different web sites (or web applications) that the presenter had created. It was interesting to compare and contrast the two frameworks and see how they differed or stayed the same.

We are currently looking for presenters for our August and September meetings, so if you want to talk about how you’re using Python now, in the past or even what you plan to do with it in the future, let me know by emailing me or in the comments!