Skip to main content

fetch data from jupyter notebook

Project description

jupyter-data-fetch

jupyterlabjupyter notebook中抓取数据的示例

优点

  1. ksrpc比,通用性更强,理论上全平台通用
  2. 不需中转服务器,网页能打开就能使用

缺点

  1. ksrpc传输是二进制,而本项目编码成了base85/base64,速度较慢
  2. 传输带宽消耗多,base64多占33%,base85多占25%

安装

  1. uv pip install jupyter-data-fetch -U

使用方法

  1. examples下提供了示例
  2. joinquant为例,打开浏览器,登录研究环境,按F12打开开发者工具
  3. 搜索kernels,复制请求URLCookie devtool.png
  4. 替换示例中Cookieserver_url即可 ide.png
  5. 留意:server_url只复制一段。Cookie要完整复制

最简示例

from jupyter_kernel_client import KernelClient

from jupyter_data_fetch.codec import JupyterTextCodec

# ... 省去部分代码。更多参考examples/joinquant.py

with KernelClient(server_url="https://www.joinquant.com/user/12345678901", token=None, headers=headers) as kernel:
    # 一定要保证缩进正确
    code = """
df = get_fundamentals(query(
        valuation, income
    ).filter(
        # 这里不能使用 in 操作, 要使用in_()函数
        valuation.code.in_(['000001.XSHE', '600000.XSHG'])
    ), date='2015-10-15')
"""
    reply = kernel.execute(JupyterTextCodec.generate_code(code, var_name='df'), store_history=False)
    # print(reply)
    obj = JupyterTextCodec.extract_decode(reply)
    print(obj)

进阶函数

  1. 由于code是字符串,动态传入list/dict太麻烦,所以还提供了auto_execute
  2. 用户提前对函数套上auto_execute装饰器,就可以快速使用
  3. 参考examples/jqresearch.py

自动登录并获取数据的完整示例

参考examples/playwright/joinquant.py

核心代码

  1. JupyterTextCodec: 目前使用base85编解码器,使用字符串传输数据,压缩率高。字符串被截断时,必须使用JupyterImageCodec
  2. JupyterImageCodec: 图片编解码器,使用图片传输数据,base64编码压缩率低
  3. generate_code生成可在Notebook单元格中运行的代码字符串,一定要指定需要获取的变量名var_name
  4. kernel.execute在服务段执行字符串代码,返回json对象
  5. extract_decodejson中提取数据后解码成对象

注意

  1. 由于各平台限制,generate_code生成的代码可能无法运行,可以复制到Notebook中测试
  2. python3.6问题太多,可以打开一个ipynb文件后,通过菜单更改内核为最新版
  3. 可以连接到已经打开的内核,只要提供kernel_id参数即可。参考ricequant.py示例
  4. Notebook中可以导入当前目录中py,但本项目直接使用当前目录是/,导致导入失败,通过指定kernel_id可解决

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

jupyter_data_fetch-0.1.1.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

jupyter_data_fetch-0.1.1-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

Details for the file jupyter_data_fetch-0.1.1.tar.gz.

File metadata

  • Download URL: jupyter_data_fetch-0.1.1.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.25

File hashes

Hashes for jupyter_data_fetch-0.1.1.tar.gz
Algorithm Hash digest
SHA256 7d2a0f0f27aa3f62bc58e0592e22fe271b35503520c7f24465dcb2d9e651c26e
MD5 8c429354f2446b32616d0dd227dc4112
BLAKE2b-256 334f85927fda912c0241c5eb17e067674e67d4084f11c393573047b7cf70affa

See more details on using hashes here.

File details

Details for the file jupyter_data_fetch-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for jupyter_data_fetch-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 638dcc5c53fdeec52fe4b95089ddf276153b7b3aa67f973a8498e177876aa686
MD5 e212de4bf3c913eb57e8827aa0113cb5
BLAKE2b-256 d53f0909e27ec8f2b2932477be7a374820ebba8e8d8f8c6a1a5575ec9f1854d0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page