Skip to content

Commit dce0a3f

Browse files
committed
feat: add SVG format support for view and custom view images
1 parent 2f405f7 commit dce0a3f

File tree

5 files changed

+154
-6
lines changed

5 files changed

+154
-6
lines changed

tableauserverclient/server/endpoint/custom_views_endpoint.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ def populate_image(self, view_item: CustomViewItem, req_options: Optional["Image
121121
view_item : CustomViewItem
122122
123123
req_options : ImageRequestOptions, optional
124-
Options to customize the image returned, by default None
124+
Options to customize the image returned, including format (PNG or SVG), by default None
125125
126126
Returns
127127
-------
@@ -139,6 +139,13 @@ def populate_image(self, view_item: CustomViewItem, req_options: Optional["Image
139139
def image_fetcher():
140140
return self._get_view_image(view_item, req_options)
141141

142+
if req_options is not None:
143+
if not self.parent_srv.check_at_least_version("3.29"):
144+
if req_options.format:
145+
from tableauserverclient.server.endpoint.exceptions import UnsupportedAttributeError
146+
147+
raise UnsupportedAttributeError("format parameter is only supported in 3.29+")
148+
142149
view_item._set_image(image_fetcher)
143150
logger.info(f"Populated image for custom view (ID: {view_item.id})")
144151

tableauserverclient/server/endpoint/views_endpoint.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ def populate_image(self, view_item: ViewItem, req_options: Optional["ImageReques
158158
159159
req_options: Optional[ImageRequestOptions], default None
160160
Optional request options for the request. These options can include
161-
parameters such as image resolution and max age.
161+
parameters such as image resolution, max age, and format (PNG or SVG).
162162
163163
Returns
164164
-------
@@ -171,9 +171,13 @@ def populate_image(self, view_item: ViewItem, req_options: Optional["ImageReques
171171
def image_fetcher():
172172
return self._get_view_image(view_item, req_options)
173173

174-
if not self.parent_srv.check_at_least_version("3.23") and req_options is not None:
175-
if req_options.viz_height or req_options.viz_width:
176-
raise UnsupportedAttributeError("viz_height and viz_width are only supported in 3.23+")
174+
if req_options is not None:
175+
if not self.parent_srv.check_at_least_version("3.23"):
176+
if req_options.viz_height or req_options.viz_width:
177+
raise UnsupportedAttributeError("viz_height and viz_width are only supported in 3.23+")
178+
if not self.parent_srv.check_at_least_version("3.29"):
179+
if req_options.format:
180+
raise UnsupportedAttributeError("format parameter is only supported in 3.29+")
177181

178182
view_item._set_image(image_fetcher)
179183
logger.info(f"Populated image for view (ID: {view_item.id})")

tableauserverclient/server/request_options.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,10 @@ class ImageRequestOptions(_ImagePDFCommonExportOptions):
497497
viz_width: int, optional
498498
The width of the viz in pixels. If specified, viz_height must also be specified.
499499
500+
format: str, optional
501+
The format of the image to export. Use Format.PNG, Format.SVG, Format.png, or Format.svg.
502+
Default is "PNG". Available in API version 3.29+.
503+
500504
"""
501505

502506
extension = "png"
@@ -505,14 +509,23 @@ class ImageRequestOptions(_ImagePDFCommonExportOptions):
505509
class Resolution:
506510
High = "high"
507511

508-
def __init__(self, imageresolution=None, maxage=-1, viz_height=None, viz_width=None):
512+
class Format:
513+
PNG = "PNG"
514+
SVG = "SVG"
515+
png = "PNG"
516+
svg = "SVG"
517+
518+
def __init__(self, imageresolution=None, maxage=-1, viz_height=None, viz_width=None, format=None):
509519
super().__init__(maxage=maxage, viz_height=viz_height, viz_width=viz_width)
510520
self.image_resolution = imageresolution
521+
self.format = format
511522

512523
def get_query_params(self):
513524
params = super().get_query_params()
514525
if self.image_resolution:
515526
params["resolution"] = self.image_resolution
527+
if self.format:
528+
params["format"] = self.format
516529
return params
517530

518531

test/test_custom_view.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,69 @@ def test_populate_image_with_options(server: TSC.Server) -> None:
116116
assert response == single_view.image
117117

118118

119+
def test_populate_image_svg_format(server: TSC.Server) -> None:
120+
server.version = "3.29"
121+
response = b"<svg>test</svg>"
122+
with requests_mock.mock() as m:
123+
m.get(
124+
server.custom_views.baseurl + "/d79634e1-6063-4ec9-95ff-50acbf609ff5/image?format=SVG",
125+
content=response,
126+
)
127+
single_view = TSC.CustomViewItem()
128+
single_view._id = "d79634e1-6063-4ec9-95ff-50acbf609ff5"
129+
req_option = TSC.ImageRequestOptions(format=TSC.ImageRequestOptions.Format.SVG)
130+
server.custom_views.populate_image(single_view, req_option)
131+
assert response == single_view.image
132+
133+
134+
def test_populate_image_png_format(server: TSC.Server) -> None:
135+
server.version = "3.29"
136+
response = POPULATE_PREVIEW_IMAGE.read_bytes()
137+
with requests_mock.mock() as m:
138+
m.get(
139+
server.custom_views.baseurl + "/d79634e1-6063-4ec9-95ff-50acbf609ff5/image?format=PNG",
140+
content=response,
141+
)
142+
single_view = TSC.CustomViewItem()
143+
single_view._id = "d79634e1-6063-4ec9-95ff-50acbf609ff5"
144+
req_option = TSC.ImageRequestOptions(format=TSC.ImageRequestOptions.Format.PNG)
145+
server.custom_views.populate_image(single_view, req_option)
146+
assert response == single_view.image
147+
148+
149+
def test_populate_image_svg_format_lowercase_alias(server: TSC.Server) -> None:
150+
server.version = "3.29"
151+
response = b"<svg>test</svg>"
152+
with requests_mock.mock() as m:
153+
m.get(
154+
server.custom_views.baseurl + "/d79634e1-6063-4ec9-95ff-50acbf609ff5/image?format=SVG",
155+
content=response,
156+
)
157+
single_view = TSC.CustomViewItem()
158+
single_view._id = "d79634e1-6063-4ec9-95ff-50acbf609ff5"
159+
req_option = TSC.ImageRequestOptions(format=TSC.ImageRequestOptions.Format.svg)
160+
server.custom_views.populate_image(single_view, req_option)
161+
assert response == single_view.image
162+
163+
164+
def test_populate_image_format_unsupported_version(server: TSC.Server) -> None:
165+
from tableauserverclient.server.endpoint.exceptions import UnsupportedAttributeError
166+
167+
server.version = "3.28"
168+
response = POPULATE_PREVIEW_IMAGE.read_bytes()
169+
with requests_mock.mock() as m:
170+
m.get(
171+
server.custom_views.baseurl + "/d79634e1-6063-4ec9-95ff-50acbf609ff5/image?format=SVG",
172+
content=response,
173+
)
174+
single_view = TSC.CustomViewItem()
175+
single_view._id = "d79634e1-6063-4ec9-95ff-50acbf609ff5"
176+
req_option = TSC.ImageRequestOptions(format=TSC.ImageRequestOptions.Format.SVG)
177+
178+
with pytest.raises(UnsupportedAttributeError):
179+
server.custom_views.populate_image(single_view, req_option)
180+
181+
119182
def test_populate_image_missing_id(server: TSC.Server) -> None:
120183
single_view = TSC.CustomViewItem()
121184
single_view._id = None

test/test_view.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,67 @@ def test_populate_image_with_options(server: TSC.Server) -> None:
238238
assert response == single_view.image
239239

240240

241+
def test_populate_image_svg_format(server: TSC.Server) -> None:
242+
server.version = "3.29"
243+
response = b"<svg>test</svg>"
244+
with requests_mock.mock() as m:
245+
m.get(
246+
server.views.baseurl + "/d79634e1-6063-4ec9-95ff-50acbf609ff5/image?format=SVG",
247+
content=response,
248+
)
249+
single_view = TSC.ViewItem()
250+
single_view._id = "d79634e1-6063-4ec9-95ff-50acbf609ff5"
251+
req_option = TSC.ImageRequestOptions(format=TSC.ImageRequestOptions.Format.SVG)
252+
server.views.populate_image(single_view, req_option)
253+
assert response == single_view.image
254+
255+
256+
def test_populate_image_png_format(server: TSC.Server) -> None:
257+
server.version = "3.29"
258+
response = POPULATE_PREVIEW_IMAGE.read_bytes()
259+
with requests_mock.mock() as m:
260+
m.get(
261+
server.views.baseurl + "/d79634e1-6063-4ec9-95ff-50acbf609ff5/image?format=PNG",
262+
content=response,
263+
)
264+
single_view = TSC.ViewItem()
265+
single_view._id = "d79634e1-6063-4ec9-95ff-50acbf609ff5"
266+
req_option = TSC.ImageRequestOptions(format=TSC.ImageRequestOptions.Format.PNG)
267+
server.views.populate_image(single_view, req_option)
268+
assert response == single_view.image
269+
270+
271+
def test_populate_image_svg_format_lowercase_alias(server: TSC.Server) -> None:
272+
server.version = "3.29"
273+
response = b"<svg>test</svg>"
274+
with requests_mock.mock() as m:
275+
m.get(
276+
server.views.baseurl + "/d79634e1-6063-4ec9-95ff-50acbf609ff5/image?format=SVG",
277+
content=response,
278+
)
279+
single_view = TSC.ViewItem()
280+
single_view._id = "d79634e1-6063-4ec9-95ff-50acbf609ff5"
281+
req_option = TSC.ImageRequestOptions(format=TSC.ImageRequestOptions.Format.svg)
282+
server.views.populate_image(single_view, req_option)
283+
assert response == single_view.image
284+
285+
286+
def test_populate_image_format_unsupported_version(server: TSC.Server) -> None:
287+
server.version = "3.28"
288+
response = POPULATE_PREVIEW_IMAGE.read_bytes()
289+
with requests_mock.mock() as m:
290+
m.get(
291+
server.views.baseurl + "/d79634e1-6063-4ec9-95ff-50acbf609ff5/image?format=SVG",
292+
content=response,
293+
)
294+
single_view = TSC.ViewItem()
295+
single_view._id = "d79634e1-6063-4ec9-95ff-50acbf609ff5"
296+
req_option = TSC.ImageRequestOptions(format=TSC.ImageRequestOptions.Format.SVG)
297+
298+
with pytest.raises(UnsupportedAttributeError):
299+
server.views.populate_image(single_view, req_option)
300+
301+
241302
def test_populate_pdf(server: TSC.Server) -> None:
242303
response = POPULATE_PDF.read_bytes()
243304
with requests_mock.mock() as m:

0 commit comments

Comments
 (0)