首页 > 其他分享 >SciTech-BigDataAIML-Jupyter- 扩展Jupyter Notebook 的文档 Extending the Notebook

SciTech-BigDataAIML-Jupyter- 扩展Jupyter Notebook 的文档 Extending the Notebook

时间:2023-12-30 23:55:05浏览次数:32  
标签:Jupyter extension handlers app notebook server Extending Notebook

https://jupyterlab-lsp.readthedocs.io/en/latest/Installation.html
https://github.com/jupyter-lsp/jupyterlab-lsp

Docs » Extending the Notebook » Custom request handlers Edit on GitHub
Note

You are not reading the most recent version of this documentation. v7.0.6 is the latest version available.
Custom request handlers

The notebook webserver can be interacted with using a well defined RESTful API. You can define custom RESTful API handlers in addition to the ones provided by the notebook. As described below, to define a custom handler you need to first write a notebook server extension. Then, in the extension, you can register the custom handler.

Writing a notebook server extension

The notebook webserver is written in Python, hence your server extension should be written in Python too. Server extensions, like IPython extensions, are Python modules that define a specially named load function, load_jupyter_server_extension. This function is called when the extension is loaded.

def load_jupyter_server_extension(nb_server_app):
"""
Called when the extension is loaded.

Args:
    nb_server_app (NotebookWebApplication): handle to the Notebook webserver instance.
"""
pass

To get the notebook server to load your custom extension, you’ll need to add it to the list of extensions to be loaded. You can do this using the config system. NotebookApp.server_extensions is a config variable which is an array of strings, each a Python module to be imported. Because this variable is notebook config, you can set it two different ways, using config files or via the command line.

For example, to get your extension to load via the command line add a double dash before the variable name, and put the Python array in double quotes. If your package is “mypackage” and module is “mymodule”, this would look like jupyter notebook --NotebookApp.server_extensions="['mypackage.mymodule']" . Basically the string should be Python importable.

Alternatively, you can have your extension loaded regardless of the command line args by setting the variable in the Jupyter config file. The default location of the Jupyter config file is ~/.jupyter/profile_default/jupyter_notebook_config.py. Then, inside the config file, you can use Python to set the variable. For example, the following config does the same as the previous command line example [1].

c = get_config()
c.NotebookApp.server_extensions = [
'mypackage.mymodule'
]
Before continuing, it’s a good idea to verify that your extension is being loaded. Use a print statement to print something unique. Launch the notebook server and you should see your statement printed to the console.

Registering custom handlers

Once you’ve defined a server extension, you can register custom handlers because you have a handle to the Notebook server app instance (nb_server_app above). However, you first need to define your custom handler. To declare a custom handler, inherit from notebook.base.handlers.IPythonHandler. The example below[1] is a Hello World handler:

from notebook.base.handlers import IPythonHandler

class HelloWorldHandler(IPythonHandler):
def get(self):
self.finish('Hello, world!')
The Jupyter Notebook server use Tornado as its web framework. For more information on how to implement request handlers, refer to the Tornado documentation on the matter.

After defining the handler, you need to register the handler with the Notebook server. See the following example:

web_app = nb_server_app.web_app
host_pattern = '.*$'
route_pattern = url_path_join(web_app.settings['base_url'], '/hello')
web_app.add_handlers(host_pattern, [(route_pattern, HelloWorldHandler)])
Putting this together with the extension code, the example looks like the following:

from notebook.utils import url_path_join
from notebook.base.handlers import IPythonHandler

class HelloWorldHandler(IPythonHandler):
def get(self):
self.finish('Hello, world!')

def load_jupyter_server_extension(nb_server_app):
"""
Called when the extension is loaded.

Args:
    nb_server_app (NotebookWebApplication): handle to the Notebook webserver instance.
"""
web_app = nb_server_app.web_app
host_pattern = '.*$'
route_pattern = url_path_join(web_app.settings['base_url'], '/hello')
web_app.add_handlers(host_pattern, [(route_pattern, HelloWorldHandler)])

References: 1. Peter Parente’s Mindtrove

© Copyright 2015, Jupyter Team, https://jupyter.org. Revision 2c96ad2d.

标签:Jupyter,extension,handlers,app,notebook,server,Extending,Notebook
From: https://www.cnblogs.com/abaelhe/p/17937090

相关文章

  • Jupyter 安装
    最近想折腾下数据分析,重拾python,遂有了这篇文章,以此开启记录学习过程。一、安装网上有很多安装教程,起初为了图便利是直接通过pip来安装的,mac通过pip安装pip3installjupyter但是在新建python文件时会报500服务器内部错误,服务端日志打印了一个permissiondeny的错误,试了网......
  • Jupyter Notebook 安装使用及快捷键
    ​  参考文档:JupyterNotebook安装使用及快捷键1、安装条件安装JupyterNotebook之前需要已经安装Python(3.3版本及以上,或2.7版本)。如若初学者可以参考下面使用Anaconda安装的方法。2、使用pip安装JupyterNotebook安装JupyterNotebook最简单的方法是使用pip包管理......
  • jupyter
    1、安装:pip3installjupyter2、配置:生成配置文件:jupyter-lab--generate-config查看配置文件路径:jupyter--config-dir查看配置:jupyter-lab--show-config修改相关配置:c.ServerApp.ip='192.168.1.2'#服务器侦听地址c.ServerApp.port=8080#侦听端口,默认8888c.Se......
  • python notebook
    python(11.20--12.7)配置python环境:下载:anaconda在清华大学开源软件镜像站中选择合适的镜像进行下载。anaconda的存在和作用:要使用anaconda不妨先了解anaconda是什么,做什么。首先要知道,anaconda的存在必然有其存在的社会条件,现如今的python拥有众多版本,他们可以满足使用者不......
  • P6入门:项目初始化3-项目详情之记事本Notebook
    前言使用项目详细信息查看和编辑有关所选项目的详细信息,在项目创建完成后,初始化项目是一项非常重要的工作,涉及需要设置的内容包括项目名,ID,责任人,日历,预算,资金,分类码等等,在接下来的博文中,我将结合官方帮助介绍这些基本设置,希望给对P6感兴趣的人带来帮助。涉及P6 项目详情设置包括:G......
  • jupyter notebook代码补全扩展安装遇到 Jupyter command `jupyter-contrib` not found
    Jupytercommandjupyter-contribnotfound.解决方案——新的安装方式。方法1:pip方式1.先使用以下命令,卸载旧版本的jupyter_contrib_nbextensions和upyter_nbextensions_configurator:分别用cmd命令,卸载之前的安装pipuninstalljupyter_contrib_nbextensionspipuninsta......
  • hackthebox jupyter medium
    BREIFLY.thisboxisquitehardforbeginner.thewalkthroughisfollowing:1.nmapscanopenportsdetailanddiscoverthisboxopen22and80portbutonlygivethedomain http://jupiter.htb FUZZTESTING:atthetimewecanFUZZthesubdomainofthisdom......
  • Jupyter Notebook 使用与安装
    简介JupyterNotebook就是以网页的形式打开,可以在网页页面中直接编写代码和运行代码,代码的运行结果也会直接在代码块下显示的程序。如在编程过程中需要编写说明文档,可在同一个页面中直接编写,便于作及时的说明和解释。官网:https://jupyter-notebook.readthedocs.io/en/stable/not......
  • 设置jupyter中DataFrame的显示限制方式
    jupyter中显示的DataFrame过长时会自动换行(print()显示方式)或自动省略(单元格最后一行直接显示),在一些情况下看上去不是很方便,可调节显示参数如下:importpandasaspdpd.set_option('display.width',500)#设置整体宽度pd.set_option('display.height',500)#设置整体高......
  • Jupyter Notebook的使用
    什么是Jupyter NotebookJupyterNotebook是一个基于Web的交互式计算环境,支持多种编程语言,包括Python、R、Julia等。它的主要功能是将代码、文本、数学方程式、可视化和其他相关元素组合在一起,创建一个动态文档,用于数据分析、机器学习、科学计算和数据可视化等方面。JupyterN......