Android – Designing Flask apps with mobile devices in mind?

Designing Flask apps with mobile devices in mind?… here is a solution to the problem.

Designing Flask apps with mobile devices in mind?

I’m reading about Flask. Given its tight integration with Jinja2 and WTF forms, what happens when I start writing a native mobile version of my website? I usually write a bunch of backend APIs that work independently of the front-end and then write the front-end code using JS. That way, if I have to implement a native mobile app, I can use the backend APIs seamlessly. Flask (or some other framework) is tightly integrated with the template engine, how should I design my application?

For example, let’s take here as an example, and the author advocates that the login function be written like this:

from flask import render_template, flash, redirect
from app import app
from forms import LoginForm

# index view function suppressed for brevity

@app.route('/login', methods = ['GET', 'POST'])
def login():
    form = LoginForm()
    if form.validate_on_submit():
        flash('Login requested for OpenID="' + form.openid.data + '", remember_me=' + str(form.remember_me.data))
        return redirect('/index')
    return render_template('login.html', 
        title = 'Sign In',
        form = form)

However, when I build a native Android/iOS app, I assume that the backend should expose a bunch of API calls to validate the input and sign in for you. Given that mobile devices are agnostic to Jinga2 or some other template (since everything is natively implemented), all this code is useless in the context of a native mobile application. This means that I will have to refactor the “real world” Flask code to be compatible with mobile apps. Is this the case or am I missing out on a higher-level perspective?

My specific question is: what design patterns should I follow in Flask to ensure my website is web and mobile friendly?

Solution

I think there are two problems here:

  1. Write a web and mobile-friendly web client
  2. Design apps using web and mobile components

Question 1 involves responsive web design, formatting web pages in a way that is friendly to both desktop web browsers and mobile web browsers. There are CSS techniques that use different stylesheets and templates depending on the size of the browser viewport. This will be where different jinja2 templates can be used for mobile clients and web clients. Or there is a “responsive design” that adjusts to the size of the viewport.

Issue 2 describes how you build your services and customers. You can do as you say and have a backend API that is independent of any frontend (which can be a Flask application or not.) Flask-Classy or Flask-Restful is a Flask extension that helps develop REST APIs using Flask). You can then write a native mobile application that uses the backend API. You can write Flask web applications that also use the backend. There will be no dependencies between the mobile app and the Flask app. They’re just two different clients, both accessing the same backend API.

The example you link to is creating a monolithic web application. If that’s what you’re going to create, then this is a great tutorial. However, if you want a set of services that can be consumed by mobile applications and web clients, it won’t be fully suitable.

Related Problems and Solutions