從 Airflow Web UI 自訂 Apache 檢視

Airflow 具有一項功能,允許使用外掛程式管理器將自訂 UI 與其核心 UI 整合。

這是一個 Airflow 的範例外掛程式,完全不顯示任何內容。

在此外掛程式中,兩個物件參考衍生自基底類別 airflow.plugins_manager.AirflowPlugin。它們是 flask_blueprints 和 appbuilder_views。

在 Airflow 外掛程式中使用 flask_blueprints,可以擴充核心應用程式以支援自訂應用程式來檢視 Empty Plugin。在此物件參考中,Blueprint 物件的清單包含用於呈現資訊的靜態範本。

在 Airflow 外掛程式中使用 appbuilder_views,會新增一個代表概念的類別,並使用檢視和方法來實作它。在此物件參考中,FlaskAppBuilder BaseView 物件和中繼資料資訊(例如名稱和類別)的字典清單會傳遞出去。

自訂檢視註冊

具有物件參考至 flask_appbuilder 和來自 flask 的 Blueprint 的自訂檢視可以註冊為外掛程式的一部分。

以下是我們實作新的自訂檢視的骨架。

docs/apache-airflow/empty_plugin/empty_plugin.py[原始碼]

#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
"""Plugins example"""

from __future__ import annotations

from flask import Blueprint
from flask_appbuilder import BaseView, expose

from airflow.auth.managers.models.resource_details import AccessView
from airflow.plugins_manager import AirflowPlugin
from airflow.www.auth import has_access_view


class EmptyPluginView(BaseView):
    """Creating a Flask-AppBuilder View"""

    default_view = "index"

    @expose("/")
    @has_access_view(AccessView.PLUGINS)
    def index(self):
        """Create default view"""
        return self.render_template("empty_plugin/index.html", name="Empty Plugin")


# Creating a flask blueprint
bp = Blueprint(
    "Empty Plugin",
    __name__,
    template_folder="templates",
    static_folder="static",
    static_url_path="/static/empty_plugin",
)


class EmptyPlugin(AirflowPlugin):
    """Defining the plugin class"""

    name = "Empty Plugin"
    flask_blueprints = [bp]
    appbuilder_views = [{"name": "Empty Plugin", "category": "Extra Views", "view": EmptyPluginView()}]

appbuilder_views 字典的 category 鍵中指定的外掛程式是 Airflow UI 導覽列中索引標籤的名稱。「Empty Plugin」是「Plugins」索引標籤下連結的名稱,它將啟動外掛程式。

我們需要新增 Blueprint 以產生需要在 Airflow Web UI 中呈現的應用程式部分。我們可以定義範本、靜態檔案,並且當外掛程式載入時,此 Blueprint 將註冊為 Airflow 應用程式的一部分。

具有自訂檢視 UI 的 $AIRFLOW_HOME/plugins 資料夾具有以下資料夾結構。

plugins
├── empty_plugin.py
├── templates
|   └── empty_plugin
|       ├── index.html
└── README.md

呈現建置的檢視所需的 HTML 檔案會新增為 Airflow 外掛程式的一部分,放入 $AIRFLOW_HOME/plugins/templates 資料夾中,並在 Blueprint 中定義。

這個條目有幫助嗎?