不确定这是否是您需要的,但我认为我的回答可以节省一些时间并得出一些结论,因为网络上几乎没有示例。我的示例是什么:1) 在主菜单中添加自定义下拉菜单2) 更改列编辑字段。
我找到了hook,它提供了在应用运行时做某事的可能性。
我还发现在你的情况下工作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)。您将看到我们视图的字段已更改 + 新菜单项:
如果您打开我们的菜单项(或http://0.0.0.0:8088/example),您将看到superset 的基本布局:
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 %}
但我确信定制很耗时。一些组件使用builded js packages 工作。我不能保证 UI 在 backend 修改后会正常工作。
无论如何,我希望这会对某人有所帮助。