fetch data from jupyter notebook
Project description
jupyter-data-fetch
从jupyterlab、jupyter notebook中抓取数据的示例
优点
- 与
ksrpc比,通用性更强,理论上全平台通用 - 不需中转服务器,网页能打开就能使用
缺点
ksrpc传输是二进制,而本项目编码成了base85/base64,速度较慢- 传输带宽消耗多,
base64多占33%,base85多占25%
安装
uv pip install jupyter-data-fetch -U
使用方法
examples下提供了示例- 以
joinquant为例,打开浏览器,登录研究环境,按F12打开开发者工具 - 搜索
kernels,复制请求URL和Cookie - 替换示例中
Cookie和server_url即可 - 留意:
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)
进阶函数
- 由于
code是字符串,动态传入list/dict太麻烦,所以还提供了auto_execute - 用户提前对函数套上
auto_execute装饰器,就可以快速使用 - 参考examples/jqresearch.py
自动登录并获取数据的完整示例
参考examples/playwright/joinquant.py
核心代码
JupyterTextCodec: 目前使用base85编解码器,使用字符串传输数据,压缩率高。字符串被截断时,必须使用JupyterImageCodecJupyterImageCodec: 图片编解码器,使用图片传输数据,base64编码压缩率低generate_code生成可在Notebook单元格中运行的代码字符串,一定要指定需要获取的变量名var_namekernel.execute在服务段执行字符串代码,返回json对象extract_decode从json中提取数据后解码成对象
注意
- 由于各平台限制,
generate_code生成的代码可能无法运行,可以复制到Notebook中测试 python3.6问题太多,可以打开一个ipynb文件后,通过菜单更改内核为最新版- 可以连接到已经打开的内核,只要提供
kernel_id参数即可。参考ricequant.py示例 Notebook中可以导入当前目录中py,但本项目直接使用当前目录是/,导致导入失败,通过指定kernel_id可解决
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file jupyter_data_fetch-0.1.2.tar.gz.
File metadata
- Download URL: jupyter_data_fetch-0.1.2.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
229454f6267a8e8b9d81ac7955b885a006bb8697cc7095d318b26af1d387a0a8
|
|
| MD5 |
bc4ef4cbf1c158bf8d6616cd843457d4
|
|
| BLAKE2b-256 |
c483dca4a9840328edae0d8f166265cdddb201b24367898cceafe90849de148e
|
File details
Details for the file jupyter_data_fetch-0.1.2-py3-none-any.whl.
File metadata
- Download URL: jupyter_data_fetch-0.1.2-py3-none-any.whl
- Upload date:
- Size: 7.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.25
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7eeefcbcfbc6d0edcb25d33b40c356618e77cdde732549b09113ed4a4e9157e7
|
|
| MD5 |
742f1f66e6e1a845db1e3e1b2e691ada
|
|
| BLAKE2b-256 |
47c47c93aaaba232abc180f1135b035c3da96c64db7ab6a805b1ec22d6841fc0
|