import logging import os.path import random from flask_babel import gettext as __ from flask_babel import lazy_gettext as _ from flask import g import pysnooper import datetime from flask import jsonify from myapp import conf from myapp.views.base import BaseMyappView from flask_appbuilder import expose from myapp import appbuilder from flask import stream_with_context, request class Myapp(BaseMyappView): route_base = '/myapp' default_view = 'welcome' # 设置进入蓝图的默认访问视图(没有设置网址的情况下) # @expose('/welcome') # @expose('/profile//') # def welcome(self, username=None): # if not g.user or not g.user.get_id(): # return redirect(appbuilder.get_url_for_login) # if username: # msg = 'Hello ' + username + " !" # else: # msg = 'Hello ' + g.user.username + " !" # # # 返回模板 # return self.render_template('hello.html', msg=msg) @expose('/home') def home(self): # 返回模板 return self.render_template('home.html') @expose('/navbar_right') def navbar_right(self): data = [ { "icon": '', "link": "%s/frontend/startchat/chat" % request.host_url.strip('/') }, { "icon": '', "link": "%s/frontend/ai_hub/model_market/model_visual" % request.host_url.strip('/') }, { "text": __("文档"), "icon": '', "link": conf.get('DOCUMENTATION_URL', 'https://github.com/tencentmusic/cube-studio/wiki') }, { "icon": '', "link": conf.get('BUG_REPORT_URL', '') if conf.get('BUG_REPORT_URL','') else "https://github.com/tencentmusic/cube-studio/issues/new" }, { "text": '', "icon": '', "link": "https://github.com/tencentmusic/cube-studio" } ] # data = data if conf.get('BABEL_DEFAULT_LOCALE', 'zh') == 'zh' else [] data = conf.get('NAVBAR_RIGHT',[]) if conf.get('NAVBAR_RIGHT',[]) else data # 返回模板 return jsonify(data) @expose('/menu') def menu(self): # 项目空间 projetc = { "name": 'group', "title": __('项目空间'), "isExpand": False, "isMenu": True, "icon": '', "children": [ { "name": 'project_group', "title": __('项目组'), "isMenu": True, "isExpand": True, "children": [ { "name": 'org_group', "title": __('项目分组'), "icon": '', "menu_type": "api", "url": "/project_modelview/org/api/", "model_name": "project", "related": [ { "hidden": 1, "name": 'project_user', "title": __('组成员'), "icon": '', "menu_type": "api", "url": "/project_user_modelview/api/", } ] }, { "name": 'job_template_group', "title": __('模板分类'), "icon": '', "menu_type": "api", "url": "/project_modelview/job_template/api/", "model_name": "project" } ] }, { "name": 'audit', "title": __('资源审计'), "isMenu": True, "isExpand": True, "children": [ { "name": 'pod', "title": __('计量计费'), "icon": '', "menu_type": "api", "url": "/pod_modelview/api/", "disable":True }, { "name": 'approval', "title": __('申请审批'), "icon": '', "menu_type": "api", "url": "/approval_modelview/api/", "disable":True } ] } ] } # 数据 data = { "name": 'data', "title": __('数据资产'), 'hidden': 0, "isMenu": True, "icon": '', "children": [ { "name": 'datasearch', "title": __('数据探索'), "isMenu": True, "isExpand": True, "children": [ { "name": 'data_search', "title": 'sqllab', # "disable": True, "icon": '', "menu_type": "innerRoute", "url": "{host}/frontend/dataSearch".format(host=request.host_url.strip('/')) } ] }, { "name": 'metadata', "title": __('元数据'), "isMenu": True, "isExpand": True, "children": [ { "name": 'metadata_table', "title": __('库表'), "icon": '', "menu_type": "api", "url": "/metadata_table_modelview/api/" }, { "name": 'data_blood', "title": '数据血缘', "icon": '', "menu_type": "api", "disable": True, "url": "/metadata_table_modelview/api/" }, { "name": 'metadata_metric', "title": __('指标'), "icon": '', "menu_type": "api", "url": "/metadata_metric_modelview/api/" }, { "name": 'metadata_dimension', "title": __('维表'), # "disable":True, "icon": '', "menu_type": "api", "url": "/dimension_table_modelview/api/" }, ] }, # { # "name": 'feast', # "title": '特征平台', # "isMenu": True, # "isExpand": True, # "children": [ # { # "name": 'feast', # "title": '特征平台(企业版)', # "icon": '', # "menu_type": "api", # "disable": True, # "url": "/feast_modelview/api/" # } # ] # }, { "name": 'media_data', "title": __('媒体数据'), "isMenu": True, "isExpand": True, "children": [ { "name": 'dataset', "title": __('数据集'), "icon": '', "menu_type": "api", "url": "/dataset_modelview/api/" }, { "name": 'label_platform', "title": __('标注平台'), "icon": '', "menu_type": "api", "disable": True, "url": "/metadata_table_modelview/api/" } ] }, ] } # 在线开发 dev = { "name": 'dev', "title": __('在线开发'), 'hidden': 0, "isExpand": True, "icon": '', "children": [ { "name": 'images', "title": __('镜像管理'), "isMenu": True, "isExpand": True, "children": [ { "name": 'docker_repository', "title": __('镜像仓库'), "icon": '', "menu_type": "api", "url": "/repository_modelview/api/" }, { "name": 'docker', "title": __('镜像构建'), "icon": '', "menu_type": "api", "url": "/docker_modelview/api/" }, { "name": 'template_images', "title": __('镜像管理'), "icon": '', "menu_type": "api", "url": "/images_modelview/api/" }, ] }, { "name": 'dev_online', "title": __('代码开发'), "isMenu": True, "isExpand": True, "children": [ { "name": 'notebook', "title": 'notebook', "icon": '', "menu_type": "api", "url": "/notebook_modelview/api/" } ] }, { "name": 'data_pipeline', "title": __('数据开发'), "isMenu": True, "isExpand": True, "children": [ { "name": 'etl_pipeline', "title": __('任务ETL'), "icon": '', "menu_type": "api", # "disable": True, "url": "/etl_pipeline_modelview/api/", }, { "name": 'task_manager', "title": __('任务管理'), # "disable": True, "icon": '', "menu_type": "api", "url": "/etl_task_modelview/api/", }, { "name": 'instance_manager', "title": __('任务实例'), "disable": True, "icon": '', "menu_type": "innerRoute", } ] }, ] } # 机器学习 ml = { "name": 'train', "title": __('模型训练'), 'hidden': 0, "isExpand": True, "icon": '', "children": [ { "name": 'train_template', "title": __('模板开发'), "isMenu": True, "isExpand": True, "children": [ { "name": 'job_template', "title": __('任务模板'), "icon": '', "menu_type": "api", "url": "/job_template_fab_modelview/api/" }, ] }, { "name": 'train_task', "title": __('任务流'), "isMenu": True, "isExpand": True, "children": [ { "name": 'pipeline', "title": __('任务流'), "icon": '', "menu_type": "api", "url": "/pipeline_modelview/api/", "model_name": "pipeline", "related": [ { "hidden": 1, "name": 'task', "title": __('任务'), "icon": '', "menu_type": "api", "url": "/task_modelview/api/", } ] }, { "name": 'runhistory', "title": __('定时调度'), "icon": '', "menu_type": "api", "url": "/runhistory_modelview/api/" }, { "name": 'workflow', "title": __('运行实例'), "icon": '', "menu_type": "api", "url": "/workflow_modelview/api/" }, ] }, { "name": 'automl', "title": __('Automl'), "isExpand": True, "isMenu": True, "children": [ { "name": 'hyperparameter_search', "title": __('超参搜索'), "icon": '', "menu_type": "api", "url": "/nni_modelview/api/" }, { "name": 'feature_search', "title": __('特征搜索'), "icon": '', "menu_type": "api", "disable":True, "url": "/nni_modelview/api/" }, { "name": 'model_compression', "title": __('模型压缩'), "icon": '', "menu_type": "api", "disable": True, "url": "/nni_modelview/api/" } ] }, ] } # 服务化 service = { "name": 'service', "title": __('服务化'), "isMenu": True, # "isExpand": True, "icon": '', "children": [ { "name": 'total_resource', "title": __('整体资源'), "icon": '', "menu_type": "api", "url": "/total_resource/api/", }, { "name": 'k8s_service', "title": __('内部服务'), "icon": '', "menu_type": "api", "url": "/service_modelview/api/" }, { "name": 'inferenceservice', "title": __('模型服务'), "isMenu": True, "isExpand": True, "children": [ { "name": 'model_manager', "title": __('模型管理'), "icon": '', "menu_type": "api", "url": "/training_model_modelview/web/api/" }, { "name": 'inferenceservice_manager', "title": __('推理服务'), "icon": '', "menu_type": "api", "url": "/inferenceservice_modelview/api/" }, ] }, # { # "name": 'model_observability', # "title": '模型可观测', # "icon": '', # "menu_type": "api", # "disable": True, # "url": "/service_pipeline_modelview/api/" # }, ] } # 应用市场 aihub = { "name": 'ai_hub', "title": 'AIHub', "isMenu": True, 'hidden': 1, "isExpand": True, "icon": '', "children": [ { "name": 'other', "title": __('gpt配置'), "isMenu": True, "isExpand": True, "children": [ { "name": 'chat', "title": __('智能体'), "icon": '', "menu_type": "api", "url": '/chat_modelview/api/', "disable": True }, { "name": 'chat-plugin', "title": __('Agent配置'), "icon": '', "menu_type": "api", "disable": True, "url": '/chat_plugin_modelview/api/', } ] }, { "name": 'dataset_market', "title": __('数据集'), "icon": '', "menu_type": "api", "disable": True, }, { "name": 'model_market', "title": __('模型市场'), "icon": '', "isMenu": True, 'hidden': 0, "isExpand": True, "children": [ { "name": 'model_visual', "title": __('视觉'), "icon": '', "menu_type": "api", "url": "/model_market/visual/api/" }, { "name": 'model_voice', "title": __('语音'), "icon": '', "menu_type": "api", "url": "/model_market/voice/api/" }, { "name": 'model_language', "title": __('自然语言'), "icon": '', "menu_type": "api", "url": "/model_market/language/api/" }, { "name": 'model_multimodal', "title": __('多模态'), "icon": '', "menu_type": "api", "url": "/model_market/multimodal/api/" }, { "name": 'model_aigc', "title": __('大模型'), "icon": '', "menu_type": "api", "url": "/model_market/aigc/api/" }, ] }, { "name": 'business_market', "title": __('需求市场'), "icon": '', "menu_type": "api", 'hidden': 0, "disable": True } ] } # chat # 隐藏菜单 chat= { "name": 'startchat', "title": __('数据智能'), "isMenu": True, 'hidden': 1, "isExpand": True, 'isMenuCollapsed': False, 'isCollapsed': True, "icon": '', "children": [ { "name": 'chat', "title": __('聊天'), "icon": '', "menu_type": "iframe", "isExpand": True, 'isMenuCollapsed': False, 'isCollapsed': True, "url": '/frontend/distChat/', } ] } is_admin = False if g.user and (g.user.username in conf.get('ADMIN_USER', '').split(',')): is_admin=True security_setting = { "name": 'security', "title": __('安全设置(管理员)'), "isMenu": True, "isExpand": True, "children": [ { "name": 'security-user', "title": __('用户列表'), "icon": '', "menu_type": "iframe", "url": '/users/list/?_flt_2_username=', "disable":not is_admin }, { "name": 'security-logs', "title": __('日志列表'), "icon": '', "menu_type": "iframe", "url": '/logmodelview/list/', "disable": not is_admin }, ] } resource_setting = { "name": 'resource', "title": __('资源配置(管理员)'), "isMenu": True, "isExpand": True, "children": [ { "name": 'resource-node', "title": __('机器资源'), "icon": '', "menu_type": "api", "url": "/node_modelview/api/", "disable": True }, { "name": 'resource-store', "title": __('存储资源'), "icon": '', "menu_type": "api", "url": "/storage_modelview/api/", "disable": True } ] } # print(request.scheme) # print(request.scheme) # print(request.scheme) # print(request.scheme) # print(request.is_secure) links = { "name": 'link', "title": __('链接(管理员)'), 'hidden': 0, "icon": '', "isMenu": True, "isExpand": True, "children": [ { "name": link.get('name', ''), "title": link.get('label', ''), "icon": '', "menu_type": "out_link", "disable": not is_admin, "url": link.get('url','') if 'http' not in link.get('url','') else link.get('url',''), } for link in conf.get('ALL_LINKS',[]) ] } print(request.host_url) projetc['children'].append(resource_setting) projetc['children'].append(security_setting) # print(aihub['children']) projetc['children'].append(links) # if conf.get('BABEL_DEFAULT_LOCALE','zh')=='en': # menu = [projetc, data, dev, ml, service] # else: menu = [projetc, data, dev, ml, service, aihub, chat] if conf.get('MENU', []): menu = conf.get('MENU', []) if conf.get('MENU_ADMIN', []) and is_admin: menu = conf.get('MENU_ADMIN', []) return jsonify(menu) @expose('/feature/check') def featureCheck(self): url = request.values.get("url", type=str, default=None) # print(conf.get('alert_config',{})) for route in conf.get('alert_config', {}): # 用户自定义目录 if url.replace("/frontend", '') == route.replace("/frontend", ''): try: return jsonify(conf.get('alert_config', {})[route]()) except Exception as e: print(e) data = { 'content': __('未能正常获取弹窗信息'), 'delay': 30000, 'hit': False, 'target': url, 'title': __('弹窗失败'), 'type': 'html', } # flash('未能正常获取弹窗信息', 'warning') return jsonify(data) # flash('xxxxxxx','success') return jsonify({}) # add_view_no_menu添加视图,但是没有菜单栏显示 appbuilder.add_view_no_menu(Myapp) from myapp.views.baseApi import ( MyappModelRestApi ) from myapp.security import MyUserRemoteUserModelView_Base, MyUser from flask_appbuilder.models.sqla.interface import SQLAInterface # from flask_appbuilder.security.views import UserModelView class MyUserRemoteUserModelView_Api(MyUserRemoteUserModelView_Base, MyappModelRestApi): datamodel = SQLAInterface(MyUser) route_base = "/users/api" appbuilder.add_api(MyUserRemoteUserModelView_Api)