pyaws-s3 1.0.6__tar.gz → 1.0.7__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {pyaws_s3-1.0.6 → pyaws_s3-1.0.7}/PKG-INFO +1 -1
- {pyaws_s3-1.0.6 → pyaws_s3-1.0.7}/pyaws_s3/s3.py +45 -3
- {pyaws_s3-1.0.6 → pyaws_s3-1.0.7}/pyaws_s3.egg-info/PKG-INFO +1 -1
- {pyaws_s3-1.0.6 → pyaws_s3-1.0.7}/pyaws_s3.egg-info/SOURCES.txt +0 -1
- {pyaws_s3-1.0.6 → pyaws_s3-1.0.7}/pyproject.toml +1 -1
- pyaws_s3-1.0.6/pyaws_s3/util.py +0 -47
- {pyaws_s3-1.0.6 → pyaws_s3-1.0.7}/LICENSE.md +0 -0
- {pyaws_s3-1.0.6 → pyaws_s3-1.0.7}/README.md +0 -0
- {pyaws_s3-1.0.6 → pyaws_s3-1.0.7}/pyaws_s3/__init__.py +0 -0
- {pyaws_s3-1.0.6 → pyaws_s3-1.0.7}/pyaws_s3.egg-info/dependency_links.txt +0 -0
- {pyaws_s3-1.0.6 → pyaws_s3-1.0.7}/pyaws_s3.egg-info/requires.txt +0 -0
- {pyaws_s3-1.0.6 → pyaws_s3-1.0.7}/pyaws_s3.egg-info/top_level.txt +0 -0
- {pyaws_s3-1.0.6 → pyaws_s3-1.0.7}/setup.cfg +0 -0
@@ -5,9 +5,9 @@ import aioboto3
|
|
5
5
|
import pandas as pd
|
6
6
|
import io
|
7
7
|
import matplotlib.pyplot as plt
|
8
|
+
from plotly.graph_objs import Figure
|
8
9
|
from pandas.plotting import table
|
9
10
|
from typing import Any, Literal
|
10
|
-
from util import bytes_from_figure, html_from_figure
|
11
11
|
from fpdf import FPDF
|
12
12
|
|
13
13
|
logger = logging.getLogger(__name__)
|
@@ -39,6 +39,48 @@ class S3Client:
|
|
39
39
|
self.region_name = kwargs.get("region_name", os.getenv("AWS_REGION"))
|
40
40
|
self.bucket_name = kwargs.get("bucket_name", os.getenv("AWS_BUCKET_NAME"))
|
41
41
|
|
42
|
+
def _bytes_from_figure(self, f: Figure, **kwargs) -> bytes:
|
43
|
+
"""
|
44
|
+
Convert a Plotly Figure to a PNG image as bytes.
|
45
|
+
|
46
|
+
Args:
|
47
|
+
f (Figure): The Plotly Figure object to be converted.
|
48
|
+
|
49
|
+
Returns:
|
50
|
+
bytes: The PNG image data as bytes.
|
51
|
+
:param f: The Plotly Figure object to be converted into a PNG image.
|
52
|
+
"""
|
53
|
+
|
54
|
+
format_file = kwargs.get("format_file", "png") # The format of the image to be converted to
|
55
|
+
width = kwargs.get("width", 640) # The width of the image in pixels
|
56
|
+
height = kwargs.get("height", 480) # The height of the image in pixels
|
57
|
+
|
58
|
+
with io.BytesIO() as bytes_buffer:
|
59
|
+
f.write_image(bytes_buffer,
|
60
|
+
format=format_file,
|
61
|
+
width = width,
|
62
|
+
height = height) # Write the figure to the bytes buffer as a PNG image
|
63
|
+
bytes_buffer.seek(0) # Reset the buffer position to the beginning
|
64
|
+
return bytes_buffer.getvalue() # Return the bytes data
|
65
|
+
|
66
|
+
def _html_from_figure(self, f: Figure) -> str:
|
67
|
+
"""
|
68
|
+
Convert a Plotly Figure to an HTML string.
|
69
|
+
|
70
|
+
Args:
|
71
|
+
f (Figure): The Plotly Figure object to be converted.
|
72
|
+
|
73
|
+
Returns:
|
74
|
+
str: The HTML representation of the figure as a string.
|
75
|
+
"""
|
76
|
+
with io.BytesIO() as bytes_buffer:
|
77
|
+
# Wrap the BytesIO with a TextIOWrapper to handle strings
|
78
|
+
with io.TextIOWrapper(bytes_buffer, encoding='utf-8') as text_buffer:
|
79
|
+
f.write_html(text_buffer) # Write the figure to the text buffer
|
80
|
+
text_buffer.flush() # Ensure all data is written
|
81
|
+
bytes_buffer.seek(0) # Reset the buffer position to the beginning
|
82
|
+
return bytes_buffer.getvalue().decode('utf-8') # Decode bytes to string and return
|
83
|
+
|
42
84
|
async def _get_s3_client_async(self) -> Any:
|
43
85
|
"""
|
44
86
|
Get an asynchronous S3 client using the provided AWS credentials and region.
|
@@ -178,12 +220,12 @@ class S3Client:
|
|
178
220
|
|
179
221
|
if format_file == "html":
|
180
222
|
# Convert the figure to SVG
|
181
|
-
file_text =
|
223
|
+
file_text = self._html_from_figure(fig)
|
182
224
|
# Upload the html text to s3
|
183
225
|
s3_resource.Bucket(self.bucket_name).Object(object_name).put(Body=file_text, ContentType=mimetypes)
|
184
226
|
else:
|
185
227
|
# Convert the figure to bytes
|
186
|
-
file_buffer =
|
228
|
+
file_buffer = self._bytes_from_figure(fig, format_file=format_file)
|
187
229
|
# Upload the image bytes to S3
|
188
230
|
s3_resource.Bucket(self.bucket_name).Object(object_name).put(Body=file_buffer, ContentType=mimetypes)
|
189
231
|
|
pyaws_s3-1.0.6/pyaws_s3/util.py
DELETED
@@ -1,47 +0,0 @@
|
|
1
|
-
import io
|
2
|
-
import logging
|
3
|
-
from plotly.graph_objs import Figure
|
4
|
-
|
5
|
-
logger = logging.getLogger(__name__)
|
6
|
-
|
7
|
-
def bytes_from_figure(f: Figure, **kwargs) -> bytes:
|
8
|
-
"""
|
9
|
-
Convert a Plotly Figure to a PNG image as bytes.
|
10
|
-
|
11
|
-
Args:
|
12
|
-
f (Figure): The Plotly Figure object to be converted.
|
13
|
-
|
14
|
-
Returns:
|
15
|
-
bytes: The PNG image data as bytes.
|
16
|
-
:param f: The Plotly Figure object to be converted into a PNG image.
|
17
|
-
"""
|
18
|
-
|
19
|
-
format_file = kwargs.get("format_file", "png") # The format of the image to be converted to
|
20
|
-
width = kwargs.get("width", 640) # The width of the image in pixels
|
21
|
-
height = kwargs.get("height", 480) # The height of the image in pixels
|
22
|
-
|
23
|
-
with io.BytesIO() as bytes_buffer:
|
24
|
-
f.write_image(bytes_buffer,
|
25
|
-
format=format_file,
|
26
|
-
width = width,
|
27
|
-
height = height) # Write the figure to the bytes buffer as a PNG image
|
28
|
-
bytes_buffer.seek(0) # Reset the buffer position to the beginning
|
29
|
-
return bytes_buffer.getvalue() # Return the bytes data
|
30
|
-
|
31
|
-
def html_from_figure(f: Figure) -> str:
|
32
|
-
"""
|
33
|
-
Convert a Plotly Figure to an HTML string.
|
34
|
-
|
35
|
-
Args:
|
36
|
-
f (Figure): The Plotly Figure object to be converted.
|
37
|
-
|
38
|
-
Returns:
|
39
|
-
str: The HTML representation of the figure as a string.
|
40
|
-
"""
|
41
|
-
with io.BytesIO() as bytes_buffer:
|
42
|
-
# Wrap the BytesIO with a TextIOWrapper to handle strings
|
43
|
-
with io.TextIOWrapper(bytes_buffer, encoding='utf-8') as text_buffer:
|
44
|
-
f.write_html(text_buffer) # Write the figure to the text buffer
|
45
|
-
text_buffer.flush() # Ensure all data is written
|
46
|
-
bytes_buffer.seek(0) # Reset the buffer position to the beginning
|
47
|
-
return bytes_buffer.getvalue().decode('utf-8') # Decode bytes to string and return
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|