此部分隐藏
一、下载Jupyter Notebook源码及安装
需要npm和node
pip install --upgrade setuptools pip git clone https://github.com/jupyter/notebook cd notebook pip install -e . npm run build
运行
npm run build:watch # juoyter notebook --port 8989 # 可指定端口 jupyter notebook
因为电脑上有Anaconda自带的Jupyter notebook,所以修改模板中
tree.html
检查是否安装成功:... <ul id="tabs" class="nav nav-tabs"> {# <li class="active"><a href="#notebooks" data-toggle="tab">{% trans %}Files{% endtrans %}</a></li>#} <li class="active"><a href="#notebooks" data-toggle="tab">{% trans %}Files2222{% endtrans %}</a></li> <li><a href="#running" data-toggle="tab">{% trans %}Running{% endtrans %}</a></li> <li><a href="#clusters" data-toggle="tab" class="clusters_tab_link" >{% trans %}Clusters{% endtrans %}</a></li> </ul> ...
因为后面Superset要连接,设置跨域及其他配置
执行
jupyter notebook --generate-config
在用户目录生成配置文件~/.jupyter/jupyter_notebook_config.py
(Windows路径C:\Users\CWJ\.jupyter\jupyter_notebook_config.py
:)
常用配置如下:# 解决跨域问题 c.NotebookApp.tornado_settings = { 'headers': { 'Content-Security-Policy': "frame-ancestors self *; report-uri /api/security/csp-report", } } # 可访问的IP地址 c.NotebookApp.ip = '*' # 端口 c.NotebookApp.port = 9123 # 启动服务端时是否打开浏览器 c.NotebookApp.open_browser = False # 去掉密码验证 c.NotebookApp.token = "" # 是否开启新建终端 c.NotebookApp.terminals_enabled = False # 是否可以通过前端修改密码 c.NotebookApp.allow_password_change = False # 前端是否展示退出按钮 c.NotebookApp.quit_button = False # 默认打开的目录路径 c.NotebookApp.notebook_dir = "workspace"
将生成的
jupyter_notebook_config.py
文件移动到incubator-superset/superset/notebook
下面,接着新建incubator-superset/superset/notebook/workspace
文件夹,用于设置为根目录,最后配置如下:# TODO CWJ # 解决跨域问题 c.NotebookApp.tornado_settings = { 'headers': { 'Content-Security-Policy': "frame-ancestors self *; report-uri /api/security/csp-report", } } # 允许所有外部IP访问 c.NotebookApp.ip = '0.0.0.0' # 运行时不打开本地浏览器 c.NotebookApp.open_browser = False # 服务端口 c.NotebookApp.port = 8888 # 允许在root用户下打开程序 c.NotebookApp.allow_root = True # 去掉密码验证 c.NotebookApp.token = "" # 是否开启新建终端 c.NotebookApp.terminals_enabled = False # 是否可以通过前端修改密码 c.NotebookApp.allow_password_change = False # 前端是否展示退出按钮 c.NotebookApp.quit_button = False # 默认打开的目录路径 c.NotebookApp.notebook_dir = "workspace"
在启动时设置跨域及其他配置:
# 在~目录启动 (superset) cwj0@ubuntu:~$ jupyter notebook --NotebookApp.tornado_settings='{"headers":{"Content-Security-Policy":"frame-ancestors self *; report-uri /api/security/csp-report"}}' --allow-root --no-browser --ip=0.0.0.0 --port=8888 --NotebookApp.token='' --NotebookApp.password='' --NotebookApp.quit_button=False
二、添加Jupyter Notebook到Superset
1.添加
superset/models/core.py
:
# TODO 新增
from flask_appbuilder.security.sqla.models import Model
from sqlalchemy import Column, Integer, ForeignKey, String, Sequence, Table
from superset.extensions import db
class JupyterModel(Model):
__tablename__ = 'Jupyter'
type = "table"
id = Column(Integer, primary_key=True)
name = Column(String(256))
info = Column(String(256))
JupyterModel.__table__.create(db.engine, checkfirst=True)
superset/views/core.py
:
# TODO 新增
from .base import DeleteMixin, SupersetModelView
from superset.models.core import JupyterModel
class JupyterView(SupersetModelView, DeleteMixin):
datamodel = SQLAInterface(JupyterModel)
label_columns = {'name':'Name', 'info':'Info'}
list_columns = ['name', 'Name']
@expose('/jupyter')
def jupyter(self):
return self.render_template('jupyter.html')
incubator-superset/superset/initialization/__init__.py:
...
appbuilder.add_link(
"Query Search",
label=_("Query History"),
href="/superset/sqllab/history/",
icon="fa-search",
category_icon="fa-flask",
category="SQL Lab",
category_label=__("SQL Lab"),
)
# TODO CWJ 新增
from superset.views.core import JupyterView
appbuilder.add_view(
JupyterView,
"jupyter",
label=__("Jupyterhub"), #修改为Jupyterhub
# label=__("Jupyter Notebook"),
href="/jupyterview/jupyter",
icon='fa-cogs',
category = "SQL Lab",
category_label = __("SQL Lab"),
)
...
superset/templates/jupyter.html
:
{% block tail_js %}
<div class="content-body">
<iframe frameborder='no' scrolling="no" style="width:100%;overflow:auto;height:100%;min-height:630px" src="http://127.0.0.1:8000" ></iframe>
</div>
{% endblock %}
注意:
接着执行
superset db migrate
superset db upgrade
最后必须刷新权限:superset init
,新增菜单项才能显示。
2.最终结果
跳转Jupyter Notebook:

跳转后的Jupyter Notebook页面:
