Skip to main content
 首页 » 编程设计

python之Flask、SAP UI5 和 MVC

2024年12月31日29lyhabc

我想构建一个 Python-Flask 应用程序并使用 SAPUI5。 我试图在 Flask 中构建这个小应用程序:https://sapui5.hana.ondemand.com/1.60.1/#/sample/sap.m.tutorial.walkthrough.05/preview

一切正常,我的代码如下所示:

app.py

from flask import Flask, render_template 
 
app = Flask(__name__) 
 
 
@app.route('/') 
def hello_world(): 
    return render_template("index.html") 
 
 
if __name__ == '__main__': 
    app.run() 

index.html

<!DOCTYPE html> 
<html> 
<head> 
    <meta http-equiv="X-UA-Compatible" content="IE=edge"> 
    <meta charset="utf-8"> 
    <title>SAPUI5 Walkthrough</title> 
    <script 
        id="sap-ui-bootstrap" 
        src="/static/sap-ui-core.js" 
        data-sap-ui-theme="sap_belize" 
        data-sap-ui-libs="sap.m" 
        data-sap-ui-compatVersion="edge" 
        data-sap-ui-preload="async" 
        data-sap-ui-resourceroots='{ 
            "static": "./" 
        }' 
        > 
    </script> 
    <script> 
        sap.ui.getCore().attachInit(function () { 
            sap.ui.xmlview({ 
                viewName: "mvc.view.App" 
            }).placeAt("content"); 
        }); 
    </script> 
</head> 
<body class="sapUiBody" id="content"> 
</body> 
</html> 

App.view.xml

<mvc:View 
    controllerName="mvc.controller.App" 
    xmlns="sap.m" 
    xmlns:mvc="sap.ui.core.mvc"> 
    <Button 
        text="Say Hello" 
        press="onShowHello"/> 
</mvc:View> 

App.controller.js

sap.ui.define([ 
    "sap/ui/core/mvc/Controller" 
], function (Controller) { 
    "use strict"; 
 
    return Controller.extend("mvc.App", { 
 
        onShowHello : function () { 
            // show a native JavaScript alert 
            /* eslint-disable no-alert */ 
            alert("Hello World"); 
            /* eslint-enable no-alert */ 
        } 
    }); 
 
}); 

我现在的问题是:如何将参数从我的 app.py 传递到 App.view.xml 和 App.controller.js?

最后我想达到这样的效果:

app.py

from flask import Flask, render_template 
 
app = Flask(__name__) 
 
 
@app.route('/') 
def hello_world(): 
    return render_template("index.html", text="Hello World", buttonlabel="Say Hello") 
 
 
if __name__ == '__main__': 
    app.run() 

App.view.xml

<mvc:View 
    controllerName="mvc.controller.App" 
    xmlns="sap.m" 
    xmlns:mvc="sap.ui.core.mvc"> 
    <Button 
        text="{{ buttonlabel }}" 
        press="onShowHello"/> 
</mvc:View> 

App.controller.js

sap.ui.define([ 
    "sap/ui/core/mvc/Controller" 
], function (Controller) { 
    "use strict"; 
 
    return Controller.extend("mvc.App", { 
 
        onShowHello : function () { 
            // show a native JavaScript alert 
            /* eslint-disable no-alert */ 
            alert({{ text }}); 
            /* eslint-enable no-alert */ 
        } 
    }); 
 
}); 

请您参考如下方法:

简短回答:UI5 不是模板引擎

长答案:UI5 应用程序由静态文件(.js、.html、.css)组成。因此,您的 Python 服务器必须按原样将它们返回给客户端。

来自 UI5 应用和 Python 服务器的任何通信都需要通过 REST 调用完成。