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
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
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.
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
Web scraping is where a programmer will write an application to download web pages and parse out specific information from them. Usually when you are scraping data you will need to make your application navigate the website programmatically. In this chapter, we will learn how to download files from the internet and parse them if need be. We will also learn how to create a simple spider that we can use to crawl a website.
Tips for Scraping
There are a few tips that we need to go over before we start scraping.
- Always check the website’s terms and conditions before you scrape them. They usually have terms that limit how often you can scrape or what you can you scrape
- Because your script will run much faster than a human can browse, make sure you don’t hammer their website with lots of requests. This may even be covered in the terms and conditions of the website.
- You can get into legal trouble if you overload a website with your requests or you attempt to use it in a way that violates the terms and conditions you agreed to.
- Websites change all the time, so your scraper will break some day. Know this: You will have to maintain your scraper if you want it to keep working.
- Unfortunately the data you get from websites can be a mess. As with any data parsing activity, you will need to clean it up to make it useful to you.
With that out of the way, let’s start scraping!
Continue reading A Simple Intro to Web Scraping with Python
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
Packt Publishing recently sent me a copy of the eBook version of Flask Framework Cookbook by Shalabh Aggarwal. I didn’t read it in its entirety as Cookbooks don’t usually make for a very interesting linear read. I just went through it and cherry picked various recipes. But before I get into too much detail, let’s do the quick review!
- Why I picked it up: I was asked by the publisher to read the book.
- Why I finished it: As already mentioned, I actually just skimmed the book and read random recipes
- Iâ€™d give it to: Someone who is new to Flask or possibly an intermediate Flask developer
Continue reading eBook Review: Flask Framework Cookbook
I don’t do a lot of plotting in my job, but I recently heard about a website called Plotly that provides a plotting service for anyone’s data. They even have a plotly package for Python (among others)! So in this article we will be learning how to plot with their package. Let’s have some fun making graphs!
Continue reading Plotting Data Online via Plotly and Python
One of my readers suggested that I should try logging my data to a web service called Loggly. As I understand it, Loggly is a way to share log data with everyone in a business so that you no longer need to log in to individual machines. They also provide graphs, filters and searches of the logs. They don’t have a Python API, but it’s still pretty easy to send data to Loggly via Pythons urllib2 module and simplejson. Also note that you can use Loggly for 30-day trial period.
Continue reading Using Python to Log Data to Loggly
I’ve been hearing some buzz about a newish web service called Twilio which allows you to send SMS and MMS messages among other things. There’s a handy Python wrapper to their REST API as well. If you sign up with Twilio, they will give you a trial account without even requiring you to provide a credit card, which I appreciated. You will receive a Twilio number that you can use for sending out your messages. Since you are using a trail account, you do have to authorize any phone numbers you want to send messages to before you can actually send a message. Let’s spend some time learning how this works!
Continue reading Python 101: How to send SMS/MMS with Twilio
Last night I received an email about a new Python-related Kickstarter. The Real Python crew added a new author to write a book entirely about Django 1.6. This is a subject that I keep meaning to get into and haven’t had the opportunity to do so. Hopefully by backing this project, I’ll finally learn Django.
I have been impressed with the quality of their previous projects, so I feel that I can safely endorse these authors. I’m sure the project will be of high quality and well worth your time and money. Plus it’s fun to support these guys who want to share their knowledge. If you’re interested in supporting the project you can go to the following address:
Note: They are already fully funded at this point and some of the support levels are already full, so if you want to get in early, now is the time!