从Airflow网页界面自定义Apache视图

Airflow 具备一项功能,可通过插件管理器将自定义UI与其核心UI集成

这是一个Airflow示例插件,它完全不显示任何内容。

在这个插件中,两个对象引用是从基类airflow.plugins_manager.AirflowPlugin派生而来的。它们是flask_blueprints和appbuilder_views

在Airflow插件中使用flask_blueprints,可以扩展核心应用程序以支持查看Empty插件的定制化应用。 在此对象参考中,列出了用于渲染信息的静态模板对应的Blueprint对象列表。

在Airflow插件中使用appbuilder_views时,会添加一个表示概念的类,并通过视图和方法来实现它。 在此对象引用中,会传递包含FlaskAppBuilder BaseView对象以及名称和类别等元数据信息的字典列表。

自定义视图注册

一个自定义视图,包含对Flask的flask_appbuilder和Blueprint的对象引用,并可以注册为plugin的一部分。

以下是我们实现一个新的自定义视图的框架:

docs/apache-airflow/empty_plugin/empty_plugin.py[source]

#
# 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()}]

Pluginsappbuilder_views 字典的 category 键中指定的是Airflow UI导航栏中的标签名称。Empty Plugin 是该标签下链接的名称,点击将启动该插件

我们需要添加Blueprint来生成需要在Airflow网页界面中呈现的应用程序部分。我们可以定义模板、静态文件,当插件加载时,这个blueprint将被注册为Airflow应用程序的一部分。

带有自定义视图UI的$AIRFLOW_HOME/plugins文件夹具有以下目录结构。

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

The HTML files required to render the views built are added as part of the Airflow plugin into $AIRFLOW_HOME/plugins/templates folder and defined in the blueprint.

这篇内容对您有帮助吗?