2

我正在尝试扩展/修改 Superset。我的目标是在“列表列”选项卡中添加带有附加属性的“编辑表”表单的修改版本。目前有[Column, Verbose Name, Type, Groupable, Filterable, Is temporal]。我想添加更多,例如“是目标”、“是预测器”等。

超集编辑表

我不确定执行此操作的最佳方法。

我认为尝试使用 Flask Blueprints 来做这件事是可行的方法,但文档只有一个非常基本的“hello world”类型示例:

from flask import Blueprint
simple_page = Blueprint('simple_page', __name__,
                        template_folder='templates',
                        url_prefix='/simple_page')
@simple_page.route('/', defaults={'page': 'index'})
@simple_page.route('/<page>')
def show(page):
    return "Ok"

我如何设置一个蓝图来继承 Superset 中的几乎所有内容,但添加该表单的修改版本以及必要的修改以将新列保存到数据库中?

我试图避免分叉和创建我自己的 Superset 修改版本,因为这将难以维护。

4

1 回答 1

4

不确定这是否是您需要的,但我认为我的回答可以节省一些时间并得出一些结论,因为网络上几乎没有示例。我的示例是什么:1)在主菜单中添加自定义下拉菜单2)更改列编辑字段。

我发现钩子提供了在应用程序运行时做某事的可能性。我还发现在您的情况下可以使用TableModelView。但columns tab可以使用TableColumnInlineView。项目结构:

├── superset_config.py
├── templates
│   ├── example.html

superset_config.py在运行应用程序时自动工作:

from flask import Blueprint, render_template, g, redirect
from flask_appbuilder import has_access


def flask_app_mutator(app):
    # my version of superset v0.29
    # be careful with imports! they are should be inside functions!
    # in other case config will not work
    from superset import appbuilder
    from superset.connectors.sqla.views import TableModelView, TableColumnInlineView
    # found our view and change something...
    for v in appbuilder.baseviews:
        if isinstance(v, TableModelView):
            table_columns = v.related_views[0]  # type: TableColumnInlineView
            table_columns.edit_columns = ['column_name', 'type']
    # add a new menu item
    appbuilder.add_link(
        'example',
        label='example',
        href='/example',
        category_icon='fa-file-text-o',
        category='Example')

# register our mutator - he will be called when app run
FLASK_APP_MUTATOR = flask_app_mutator

# just a new blue print for processing new menu item
example_bp = Blueprint(
    'example',
    __name__,
    template_folder='templates',
    static_url_path='/static/report')


@example_bp.route('/example')
def example():
    # as I wrote above - be careful with imports...
    from superset import appbuilder
    if not g.user or not g.user.get_id():
        return redirect(appbuilder.get_url_for_login)

    return render_template('example.html', appbuilder=appbuilder)


BLUEPRINTS = [example_bp]

从存储superset_config.py的目录运行我们的应用程序,登录,打开一些表进行编辑(例如:)http://0.0.0.0:8088/tablemodelview/edit/1。您将看到我们视图的字段已更改 + 新菜单项:

编辑列

superset如果您打开我们的菜单项(或http://0.0.0.0:8088/example) ,您将看到基本布局:自定义页面

示例.html

{% extends 'superset/basic.html' %}

{% block body %}
<div>Hello world</div>
{% endblock %}
{% block tail_js %}
    {{ super() }}
    <script type="text/javascript" src="{{ url_for('static', filename='appbuilder/js/jquery-latest.js') }}"></script>
    <script type="text/javascript" src="{{ url_for('static', filename='appbuilder/js/bootstrap.min.js') }}"></script>
{% endblock %}

但我确信定制是耗时的。一些组件使用构建的js 包工作。我不能保证在修改UI后会正常工作backend

无论如何,我希望这会对某人有所帮助。

于 2019-07-18T09:51:51.830 回答