在设计移动设备时考虑Flask应用程序? [英] Designing a Flask application with mobile in mind?

查看:91
本文介绍了在设计移动设备时考虑Flask应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读Flask.考虑到它与Jinja2和WTF-forms紧密集成,当我开始编写网站的本机移动版本时会发生什么?我通常会编写一堆独立于前端的后端API,然后使用JS编写前端代码.这样,如果我必须实现本机移动应用程序,那么我似乎可以使用后端API.通过Flask(或其他框架)与模板引擎的紧密集成,我应该如何设计我的应用程序?

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 API that work independent of the frontend and then code up the frontend using JS. This way, if I have to implement a native mobile app, I can seemlessly use the backend APIs. With Flask's (or some other framework's) tight integration with template engines, how should I design my application?

例如,让我们以此处,作者主张登录功能应这样写:

For example, let us take an example from here, 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)

但是,当我构建本机Android/iOS应用程序时,我假设后端应公开一堆API调用,以验证输入并为您完成登录.鉴于移动设备与Jinga2或其他模板无关(因为所有内容都是本地实现的),因此所有这些代码在本地移动应用程序的上下文中都是无用的.这意味着,我将不得不重构真实世界"的Flask代码以使其与移动应用程序兼容.是这种情况还是我错过了更高层次的观点?

However, when I am building a native Android/iOS app, I'm assuming that the backend should expose a bunch of API calls that validate the input and do the login for you. And given that mobile is agnostic to Jinga2 or some other templating (because everything is implemented native), all this code is useless in the context of native mobile apps. This means, I will have to refactor the "real-world" Flask code to be compatible with a mobile app. Is this the case or am I missing the higher-level point?

我的具体问题是:为了确保我的网站对Web和移动设备友好,我应该在Flask中遵循什么设计模式?

My specific question is: What is the design pattern I should follow in Flask to ensure that my site is web and mobile friendly?

推荐答案

我认为这里有2个问题:

I think there are 2 issues here:

  1. 编写一个对Web和移动设备友好的Web客户端
  2. 使用Web和移动组件设计应用程序

问题1将涉及一种响应式Web设计,该设计以对桌面Web浏览器和移动Web浏览器都友好的方式格式化网页.有CSS技术可以根据浏览器视口大小使用不同的样式表和模板.在这里,可以将不同的jinja2模板用于移动客户端和Web客户端.或存在根据视口大小进行调整的响应式设计".

Issue 1 would involve a responsive web design that formats the webpage in a manner friendly to both desktop web browsers and mobile web browsers. There are CSS techniques to use different style sheets and templates depending on the browser viewport size. This would be where different jinja2 templates could be used for mobile vs. web clients. Or there are "responsive designs" that adjust according to viewport size.

第2期介绍了如何设计服务和客户.您可以像您所说的那样做,并且拥有一个独立于任何前端的后端API(可以是Flask应用程序,也可以不是Flask应用程序.Flask-Classy或Flask-Restful是帮助使用Flask开发REST API的Flask扩展).然后,您可以编写使用后端API的本机移动应用程序.您可以编写也使用后端的Flask Web应用程序.移动应用程序和Flask应用程序之间不会有任何依赖关系.它们只是两个访问相同后端API的不同客户端.

Issue 2 speaks to how you architect your services and clients. You could do like you said and have a backend API (could be a Flask application or not. Flask-Classy or Flask-Restful are Flask extensions that assist in developing REST API with Flask) independent of any frontend. Then you could code a native mobile app that uses the backend API. And you could code a Flask web application that also uses the backend. There wouldn't be any dependencies between the mobile app and the Flask app. They're just two distinct clients that both access the same backend API.

您链接到的示例正在创建一个整体Web应用程序.如果您要创建的话,那将是一个很棒的教程.但是,如果您想要一套可以同时供移动应用程序和Web客户端使用的服务,则它并不能完全适用.

The example you linked to is creating a monolithic web application. It's an excellent tutorial if that's what you're looking to create. But it wouldn't apply in its entirety if you want a set of services that can be used by both mobile apps and web clients.

这篇关于在设计移动设备时考虑Flask应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆