Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add plotly #28

Merged
merged 7 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions .flake8

This file was deleted.

22 changes: 22 additions & 0 deletions .github/workflows/publish.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Release
on:
release:
types:
- created

jobs:
publish:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.12"
- name: Install Poetry
uses: snok/install-poetry@v1
- name: Publish
env:
PYPI_TOKEN: ${{ secrets.PYPI_API_TOKEN }}
run: |
poetry config pypi-token.pypi $PYPI_TOKEN
poetry publish --build
555 changes: 314 additions & 241 deletions poetry.lock

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "znjson"
version = "0.2.3"
version = "0.2.4"
description = "A Python Package to Encode/Decode some common file formats to json"
authors = ["zincwarecode <zincwarecode@gmail.com>"]
license = "Apache-2.0"
Expand All @@ -21,10 +21,11 @@ pytest = "^7"
ase = "^3.22.1"
ruff = "^0.4.6"
coverage = "^7"
plotly = "^5.23.0"

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"

[tool.ruff.lint]
select = ["I"]
select = ["I", "E", "W", "C", "F"]
40 changes: 40 additions & 0 deletions tests/converter/test_plotly_converter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import plotly.express as px
import pytest
from plotly.graph_objs import Figure


@pytest.fixture
def plotly_figure():
df = px.data.iris()
return px.scatter(df, x="sepal_width", y="sepal_length")


def test_encode_plotly(plotly_figure):
import znjson

data = znjson.dumps(plotly_figure)
assert data.startswith('{"_type": "plotly.graph_objs.Figure"')

fig = znjson.loads(data)
assert fig == plotly_figure
assert isinstance(fig, Figure)

assert fig.layout.xaxis.title.text == "sepal_width"
assert fig.layout.yaxis.title.text == "sepal_length"


def test_encode_list_plotly(plotly_figure):
import znjson

data = znjson.dumps([plotly_figure, plotly_figure])
assert data.startswith('[{"_type": "plotly.graph_objs.Figure"')

fig = znjson.loads(data)
assert fig == [plotly_figure, plotly_figure]
assert isinstance(fig[0], Figure)
assert isinstance(fig[1], Figure)

assert fig[0].layout.xaxis.title.text == "sepal_width"
assert fig[0].layout.yaxis.title.text == "sepal_length"
assert fig[1].layout.xaxis.title.text == "sepal_width"
assert fig[1].layout.yaxis.title.text == "sepal_length"
2 changes: 1 addition & 1 deletion tests/converter/test_small_numpy_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def test_decode(numpy_array):

def test_decode_float(numpy_float_array):
arr = (
'{"_type": "np.ndarray_small", "value": [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0,'
'{"_type": "np.ndarray_small", "value": [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0,' # noqa E501
" 8.0, 9.0]}"
)

Expand Down
2 changes: 1 addition & 1 deletion tests/test_znjson.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@


def test_version():
assert znjson.__version__ == "0.2.3"
assert znjson.__version__ == "0.2.4"
2 changes: 1 addition & 1 deletion znjson/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def save_to_b64(method):

@staticmethod
def load_from_b64(value, method):
"""Convert a string from memory into something that can be read e.g. by np.load"""
"""Convert a string from memory such that it can be read e.g. by np.load"""
with io.BytesIO() as file:
file.write(base64.b64decode(value))
file.seek(0)
Expand Down
10 changes: 7 additions & 3 deletions znjson/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import logging
from dataclasses import dataclass, field
from typing import List, Tuple, Type, Union
from typing import List, Optional, Tuple, Type, Union

from znjson import converter
from znjson.base import ConverterBase
Expand Down Expand Up @@ -33,8 +33,12 @@ def sort(self):

def register(
self,
obj: Union[
List[Type[ConverterBase]], Tuple[Type[ConverterBase]], Type[ConverterBase]
obj: Optional[
Union[
List[Type[ConverterBase]],
Tuple[Type[ConverterBase]],
Type[ConverterBase],
]
] = None,
):
"""register converters to be used with zn.En/DeCoder
Expand Down
12 changes: 9 additions & 3 deletions znjson/converter/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
"""Manage Converter Inputs based on available moduls"""

import contextlib

from znjson.converter.class_converter import ClassConverter
from znjson.converter.pathlib_converter import PathlibConverter

__all__ = ["PathlibConverter", "ClassConverter"]

try:
with contextlib.suppress(ModuleNotFoundError):
from znjson.converter.numpy_converter_base64 import NumpyConverter
from znjson.converter.numpy_converter_small import NumpyConverterSmall
from znjson.converter.old_converters import (
Expand All @@ -19,5 +21,9 @@
"NumpyConverterBase64",
"NumpyConverterLatin1",
]
except ModuleNotFoundError:
pass


with contextlib.suppress(ModuleNotFoundError):
from znjson.converter.plotly_converter import PlotlyConverter

__all__ += ["PlotlyConverter"]
16 changes: 16 additions & 0 deletions znjson/converter/plotly_converter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import plotly.graph_objs
import plotly.io as pio

from znjson.base import ConverterBase


class PlotlyConverter(ConverterBase):
instance = plotly.graph_objs.Figure
representation = "plotly.graph_objs.Figure"
level = 10

def encode(self, obj):
return obj.to_json()

def decode(self, value):
return pio.from_json(value)
2 changes: 1 addition & 1 deletion znjson/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def from_converters(cls, converter, add_default=False):
----------
converter: converter|list of converters to use
add_default: bool, default False
In addition to the converter argument use the config.ACTIVE_CONVERTER as well
In addition to the converter argument use the config.ACTIVE_CONVERTER

Returns
-------
Expand Down
Loading