跳转到主要内容

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")

当前支持的格式有:jpgpng

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")

当前支持的格式有:csvparquetfeatherxlsx

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 查看哈希值)

上传时间 Python 3

由以下支持