Tag Archives: Python

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

PyDev of the Week: Anthony Tuininga

This week we welcome Anthony Tuininga as our PyDev of the Week! Anthony is the creator of the cx_Freeze library among several others in the cx Suite.  You can get a feel for what he’s currently working on over on Github. Let’s take some time to get to know Anthony better!

Can you tell us a little about yourself (hobbies, education, etc):

I grew up in a small town in the central interior of British Columbia, Canada. In spite of it being a small town, my school managed to acquire a personal computer shortly after they were made available. I was fascinated and quickly became the school guru. That experience convinced me that computers and especially programming were in my future. I moved to Edmonton, Alberta, Canada in order to attend university and ended up staying there permanently. Instead of only taking computing science courses I ended up combining them with engineering and received a computer engineering degree. After university I first worked for a small consulting firm, then for a large consulting firm and am now working for the software company, Oracle, in the database group. Besides working with computers I enjoy reading and both cross-country and downhill skiing.

Continue reading PyDev of the Week: Anthony Tuininga

PyDev of the Week: Charles R Harris

This week we welcome Charles R. Harris as our PyDev of the Week. Charles is a core developer of NumPy, one of Python’s most popular scientific computing libraries. He has been working on NumPy since it was still called Numeric. He is also a core developer of SciPy. Let’s take some time to get to know Charles better!

Can you tell us a little about yourself (hobbies, education, etc):

I have an undergraduate degree in physics and a doctorate in mathematics. Like many with similar backgrounds, I ended up doing many things not directly related: optical and electrical design, spectroscopy, data analysis, and programming. My final niche before retirement was as the mathematical go to guy for the engineers that I worked with. Now that I am retired my hobbies are few, mostly reading science fiction and fantasy with a sprinkling of math and physics texts. Continue reading PyDev of the Week: Charles R Harris

PyDev of the Week: Ruud van der Ham

This week we welcome Ruud van der Ham as our PyDev of the Week! Ruud is the creator of the Salabim project. Let’s take some time getting to know him a bit better!

Can you tell us a little about yourself (hobbies, education, etc):

I have always worked in the port industry as a professional in the field of terminal planning, algorithm design, data science (although we didn’t use that term yet). Now that I am semi-retired in the south of France I am active as a consultant and as a Python programmer. I graduated from Delft University in applied math, and did a batchelor in Economics at Rotterdam University.

Why did you start using Python?

Actually it was a friend of mine who was doing very interesting projects for the Dutch railroad who introduced me to Python. And although I found the syntax a bit strange, I fell in love with it. With my experience of other languages (see below) it was not difficult to make a quick start.

What helped me enormously was the excellent iPad Python implementation (Pythonista). Actually I do a lot of development more or less simultaneously on iPad and Windows (with Wing IDE). Continue reading PyDev of the Week: Ruud van der Ham

PyDev of the Week: Adrian Rosebrock

This week we welcome Adrian Rosebrock (@PyImageSearch) as our PyDev of the Week. Adrian is the author of several books on Python and OpenCV. He is also the author of PyImageSearch.com, a very popular Python blog that focuses on computer vision. Let’s take some time to get to know him a bit better!

Can you tell us about yourself and PyImageSearch?

Hi Mike, thank you for the opportunity to be interviewed on PyDev of the Week.

My name is Adrian Rosebrock. I have Ph.D in computer science with a focus in computer vision and machine learning from the University of Maryland, Baltimore County (UMBC).

I blog over at PyImageSearch.com about Python, computer vision, and deep learning. Over the past few years running PyImageSearch I have written a handful of books/courses, including:

Continue reading PyDev of the Week: Adrian Rosebrock

wxPython: Moving items in ObjectListView

I was recently asked about how to implement drag-and-drop of items in a wx.ListCtrl or in ObjectListView. Unfortunately neither control has this built-in although I did find an article on the wxPython wiki that demonstrated one way to do drag-and-drop of the items in a ListCtrl.

However I did think that implementing some buttons to move items around in an ObjectListView widget should be fairly easy to implement. So that’s what this article will be focusing on.


Changing Item Order

If you don’t have wxPython and ObjectListView installed, then you will want to use pip to install them:

pip install wxPython objectlistview

Once that is done, open up your favorite text editor or IDE and enter the following code: Continue reading wxPython: Moving items in ObjectListView