background preloader

Designing a RESTful API with Python and Flask

Designing a RESTful API with Python and Flask
In recent years REST (REpresentational State Transfer) has emerged as the standard architectural design for web services and web APIs. In this article I'm going to show you how easy it is to create a RESTful web service using Python and the Flask microframework. What is REST? The characteristics of a REST system are defined by six design rules: Client-Server: There should be a separation between the server that offers a service, and the client that consumes it.Stateless: Each request from a client must contain all the information required by the server to carry out the request. What is a RESTful web service? The REST architecture was originally designed to fit the HTTP protocol that the world wide web uses. Central to the concept of RESTful web services is the notion of resources. The HTTP request methods are typically designed to affect a given resource in standard ways: The REST design does not require a specific format for the data provided with the requests. Designing a simple web service

How To Structure Large Flask Applications Introduction There are many methods and conventions for structuring Python web applications. Although certain frameworks are shipped with tools (for scaffolding) to automate -- and ease -- the task (and the headaches), almost all solutions rely on packaging / modularizing applications as the codebase gets distributed [logically] across related files and folders. The minimalist web application development framework Flask, has its own - blueprints. In this DigitalOcean article, we are going to see how to create an application directory, and structure it to work with re-usable components created with Flask's blueprints. Glossary 1. 2. 3. Prepare The Operating SystemSetting up Python, pip and virtualenv 4. Creating Application FolderCreating A Virtual EnvironmentCreating Application FilesInstalling Flask 5. Module BasicsModule Templates 6. Edit run.py using nanoEdit config.py using nano 7. Flask: The Minimalist Application Development Framework Our Choices In This Article Flask-SQLAlchemy Flask-WTF

Saturday morning hack: a little note-taking app with Flask A couple Saturdays ago I spent the morning hacking together a note-taking app. I'm really pleased with the result, so I thought I'd share the code in case anyone else might find it useful. The note-taking project idea came about out of necessity -- I wanted something that worked well from my phone. While I have a personal wiki site I've used for things like software installation notes or salsa recipes, I've also noticed that because it's so cumbersome to use from my phone, I often end up emailing things to myself. Here is how the app appears on a narrow screen like my phone: And here it is on my laptop: Because markdown is a bit difficult to use when you're not in a nice text editor like vim, I've added some simple toolbar buttons to the editor: If you'd just like to see the code, here is the multi-file gist. Feature review Based on the problems I outlined, the notes app needed to have to following features: The tools On the frontend, the first choice I made was to use Bootstrap. Python code

Structuring flask apps, a how-to for those coming from Django The other day a friend of mine was trying out flask-peewee and he had some questions about the best way to structure his app to avoid triggering circular imports. For someone new to flask, this can be a bit of a puzzler, especially if you're coming from django which automatically imports your modules. In this post I'll walk through how I like to structure my flask apps to avoid circular imports. I'll walk through the modules I commonly use in my apps, then show how to tie them all together and provide a single entrypoint into your app. Project layout I use a structure that may look familiar to users of the django framework: In a little bit I'll get to the reason "main.py" is the secret sauce, for now though I'll focus on the other bits. app.pymodels.pyauth.pyadmin.py / api.pyviews.pymain.py app.py Every flask application needs an "app.py", whether you call it that or not. """I keep app.py very thin.""" That's it! models.py auth.py admin.py / api.py Here is admin.py: And here is api.py: views.py

Large app how to · mitsuhiko/flask Wiki This document is an attempt to describe the first step of a large project structure with flask and some basic modules: SQLAlchemyWTForms Please feel free to fix and add your own tips. Installation Flask Flask Installation I recommend using virtualenv: it is easy and allows multiple environments on the same machine and doesn't even require you to have super user rights on the machine (as the libs are locally installed). Flask-SQLAlchemy SQLAlchemy provides an easy and advanced way to serialize your object to different types of relational databases. pip install flask-sqlalchemy More here about the Flask-SQLAlchemy package Flask-WTF WTForms provides an easy way to handle user's data submission. pip install Flask-WTF More here about the Flask-WTF package Overview Ok, so from now, we should have all the libs ready. /config.py /run.py /shell.py /app.db /app/__init__.py /app/constants.py /app/static/ For every module (or sub app... ) we'll have this file structure (here for the users module) Config #! Testing

Getting bigger with Flask | Tech & statup blog by maximebf My last post about creating websites with Flask covered the steps to create a simple application. What happens when it grows bigger? In this post I will take as example a common use case for a web app: a public section (homepage, tour, signup, login) a member only section (the app, user settings) an api Each member will have its own subdomain (ie: if my username is maximebf, I get the maximebf.example.com subdomain). I’ll assume the same file organization as I described in my previous post. Getting modular with Blueprints Flask provides a feature called Blueprints which let your organize your app as modules. I like to create a modules folder in my application directory where all my module files will be stored. example/ modules/ __init__.py public.py member.py api.py To create a module, you initializes a Blueprint object which acts in the same way as the Flask object. Each blueprint can have its own templates folder. I use the same logic for static files. Wildcard subdomains

Building websites in Python with Flask | Tech & statup blog by maximebf For some times now, I have been doing some projects in Python and some were web applications. Flask is a small framework to do exactly that and I have found it perfect for the job. It’s really easy to use, fast, has good documentation and a good community. This is the first post in a series dedicated to building websites with Python and more notably Flask. In this post, I will talk about setting up Flask with a database, using configuration environments, managing assets and deploying the app to production. First steps with Flask I use pip to install Python modules and I would strongly recommend it (as well as using virtualenv).Installing Flask is as easy as: pip install Flask Flask has an excellent quickstart tutorial so I will only do a quick overview of the basics. As a framework, Flask is similar to Sinatra in Ruby or Slim in PHP. from flask import Flask app = Flask(__name__) @app.route('/') def hello_world(): return 'Hello World!' app.run() starts the built-in web server on port 5000. <!

The Flask Mega-Tutorial, Part I: Hello, World! This is the first article in a series where I will be documenting my experience writing web applications in Python using the Flask microframework. NOTE: This article was revised in September 2014 to be in sync with current versions of Python and Flask. Here is an index of all the articles in the series that have been published to date: My background I'm a software engineer with double digit years of experience developing complex applications in several languages. In addition to Python, I've written web apps in PHP, Ruby, Smalltalk and believe it or not, also in C++. UPDATE: I have written a book titled "Flask Web Development", published in 2014 by O'Reilly Media. The application The application I'm going to develop as part of this tutorial is a decently featured microblogging server that I decided to call microblog. These are some of the topics I will cover as we make progress with this project: So as you see, I'm going pretty much for the whole thing. Requirements Installing Flask #! . Miguel

Related: