Now the tools I generally use for development is just Sublime Text 2, but any text editor is fine and if you want to go with a full IDE I highly recommend PyCharm from JetBrains. Now we’ll start with setting up a virtual environment for python and building our document structure. The reason for a virtual environment over using your systems, is well it’s just good practice! But seriously when working on anything outside of the general libraries for python it’s good to keep it contained within your own structure so you don’t have wind up with a ton of libraries hanging off your system. If you don’t have virtualenv already installed you’ll need it. Run the following in your terminal to get it:
Now we are ready. I do most of my setup within the terminal so I’m going to stay there for this blog post, and you should too unless you downloaded PyCharm in which case go ahead and setup a new project with a virtualenv of your own!
Now cd your way into the directory you wish to create this project and run the following commands
These two commands setup the virtual environment and then activate it to be your mocked system. (You can go back to your system at any time by running the command deactivate)
Now let’s start on the server, today we’ll be using Flask as it’s been my favorite mircoframework for some time especially when creating a service. It is useful for building full websites as well, but in the mobile world we are really interested in a server side api so that’s what we will be doing. To start install flask into your venv by running the following command.
Also since we are creating an API and not just a Flask website we should go ahead and add some CORs support. Luckily that’s extremely easy with Flask.
Alright, that’s it you’re ready to begin building this server out! Now next thing you want to do is create your main.py within your main project directory I generally try to scaffold my python services as follows:
Now we can actually begin building out the service. With flask all it takes to start a web service is to simply instantiate an app and then tell it to run. It’ll run locally on port 5000 and you can simply connect to it by going to localhost:5000 (127.0.0.1:5000) and wa la. I’ll start there and then move on to the endpoints. In order to instantiate an app make the following changes within your main.py.
And now you have a nice server running in python, we are done here happy coding!…. Kidding kidding we’ll do a bit more than that. If you go to the endpoint you’d get a nice Not found page. This is because we aren’t listening for anything, just taking up space. So let’s add a nice index for our server. The whole point of this exercise is to have an api endpoint, but we should always have an index at least to check if the api is still alive and well. I’ve already created a index for you and you can copy it out here if you want or build your own. Create a templates folder in your root directory and add the following file as index.html.
Also add the an additional module to import from flask, the render_template module.
And now we are ready to set up our first endpoint. This is quickly achieved in flask by simply telling our app to listen to a route. For our index case we want to listen to the default route ‘/’
And set that as our default page when we visit the address. To set up your route be sure to add the following before you actually start the server, but the syntax is pretty general you specify the route you want the server to handle followed by the method to be called when that endpoint is hit.
Which now if you start the server you will get a simple html page letting you know it’s alive and well, but now that’s out of the way let’s start setting up the actual meat of our api. We’ll set up a simple GET and POST method. We’ll have our server return some static data, but you’ll get the picture here for any further development. To set up a endpoint to handle a GET, POST, PUT, or DELETE you need to tell it that this is what it’ll need to do. We’ll set up our GET and POST to be handled within the same method since they are generally dealing with the same api. You can do that by adding the following endpoint to your main. This’ll is just a general endpoint for handling a request and at least getting you off the ground and started with your python api.
I hope this helped you get setup with the general structure of building a python api. I find it faster than building a quick node api although each language has their strong points. One of the cool features about working with flask as well is having it reload every time you make a change to the code and that is right out of the box without the need for browsersync.