Dash的IO简化API原型
项目描述
dash-io
Dash的IO简化API原型。这是一个实验性库,并非官方Plotly产品。
快速入门
安装库
pip install dash-io
在Python中使用它
import dash_io as dio
# ...
url_df = dio.url_from_pandas(df) # dataframe
url_im = dio.url_from_pillow(im) # PIL image
# ...
df = dio.url_to_pandas(url_df)
im = dio.url_to_pillow(url_im)
使用方法
Pillow
from PIL import Image
import numpy as np
import dash_io as dio
# Dummy image in Pillow
im = Image.fromarray(np.random.randint(0, 255, (100,100,3)))
# Encode the image into a data url
data_url = dio.url_from_pillow(im, format="jpg")
# Decode the data url into a PIL image
im = dio.url_to_pillow(data_url, format="jpg")
当前支持的格式有:jpg
、png
。
Pandas
如果您使用xlsx
,请确保已安装第三方引擎,如openpyxl
。
在Pandas中使用
import pandas as pd
import dash_io as dio
# Dummy data
data = {'col_1': [3, 2, 1, 0], 'col_2': ['a', 'b', 'c', 'd']}
df = pd.DataFrame.from_dict(data)
# To encode/decode in binary CSV format
encoded = dio.url_from_pandas(df, format="csv", index=False)
decoded = dio.url_to_pandas(encoded, format="csv")
# To encode/decode in binary parquet format
encoded = dio.url_from_pandas(df, format="parquet")
decoded = dio.url_to_pandas(encoded, format="parquet")
# To encode/decode in string CSV format (i.e. text/csv MIME type)
encoded = dio.url_from_pandas(df, format="csv", mime_type="text", mime_subtype="csv", index=False)
decoded = dio.url_to_pandas(encoded, format="csv")
当前支持的格式有:csv
、parquet
、feather
、xlsx
。
JSON
import dash_io as dio
# Encode/decode dictionary
data = {'col_1': [3, 2, 1, 0], 'col_2': ['a', 'b', 'c', 'd']}
encoded = dio.url_from_json(data)
decoded = dio.url_to_json(encoded)
# It also works with lists and other JSON-serializable objects
encoded = dio.url_from_json([1,2,3,4,5])
注意,如果dict
键是整数,它将被json
转换为字符串。这是正常行为。
Numpy
默认情况下,numpy
数组不包含MIME头。但是,您可以通过设置header=True
来启用它(例如,如果您想上传/下载一个npy
文件)。
import dash_io as dio
# Encode/decode numpy arrays without MIME header by default
array = np.array([[1, 2, 3], [4, 5, 6]])
encoded = dio.url_from_numpy(array)
decoded = dio.url_to_numpy(encoded)
# You can also use headers
encoded = dio.url_from_numpy(array, header=True)
decoded = dio.url_to_numpy(encoded, header=True)
请注意,出于安全原因,npy
文件已禁用pickle。
文档
您可以通过调用
import dash_io as dio
help(dio)
您可以在DOCS.txt
中找到最新的help
输出。
开发
首先,克隆此仓库
git clone https://github.com/plotly/dash-io
测试
创建一个虚拟环境
python -m venv venv
source venv/bin/activate
安装开发依赖项
cd dash-io
pip install requirements-dev.txt
运行pytest
python -m pytest
项目详情
下载文件
下载适用于您的平台文件。如果您不确定选择哪个,请了解更多关于安装包的信息。
源分布
dash-io-0.0.2.tar.gz (5.3 kB 查看哈希值)
构建分布
dash_io-0.0.2-py3-none-any.whl (5.2 kB 查看哈希值)
关闭
dash-io-0.0.2.tar.gz的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 5126fa6a3e0d706f77c2d938e6febc42fa7f5dd826969d8af4ece5d9a67f687d |
|
MD5 | 02ad93a0e376e9d1f827efbfeeafb83d |
|
BLAKE2b-256 | 2515546fe74d1537476116233d74d7f554a2d2c945caf37d83158b2c30516256 |
关闭
dash_io-0.0.2-py3-none-any.whl的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 96120b748c8d8ceaf0d7460c33c5581619de6838e16e6a2f46d383861ab0ecd8 |
|
MD5 | 781dad9ce735511be9d0f1f50912fbb8 |
|
BLAKE2b-256 | 6f3f69e0b3208f98fd353d26adc499c9de4f9f078d96312bcf82a285d62502a5 |