Skip to content

Commit 6282cdb

Browse files
RenaSpbclaude
andauthored
feat: add SVG format support for view and custom view images (#1755)
* feat: add SVG format support for view and custom view images Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent 2f405f7 commit 6282cdb

File tree

5 files changed

+122
-6
lines changed

5 files changed

+122
-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: 12 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,21 @@ 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+
516+
def __init__(self, imageresolution=None, maxage=-1, viz_height=None, viz_width=None, format=None):
509517
super().__init__(maxage=maxage, viz_height=viz_height, viz_width=viz_width)
510518
self.image_resolution = imageresolution
519+
self.format = format
511520

512521
def get_query_params(self):
513522
params = super().get_query_params()
514523
if self.image_resolution:
515524
params["resolution"] = self.image_resolution
525+
if self.format:
526+
params["format"] = self.format
516527
return params
517528

518529

test/test_custom_view.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,54 @@ 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_format_unsupported_version(server: TSC.Server) -> None:
150+
from tableauserverclient.server.endpoint.exceptions import UnsupportedAttributeError
151+
152+
server.version = "3.28"
153+
response = POPULATE_PREVIEW_IMAGE.read_bytes()
154+
with requests_mock.mock() as m:
155+
m.get(
156+
server.custom_views.baseurl + "/d79634e1-6063-4ec9-95ff-50acbf609ff5/image?format=SVG",
157+
content=response,
158+
)
159+
single_view = TSC.CustomViewItem()
160+
single_view._id = "d79634e1-6063-4ec9-95ff-50acbf609ff5"
161+
req_option = TSC.ImageRequestOptions(format=TSC.ImageRequestOptions.Format.SVG)
162+
163+
with pytest.raises(UnsupportedAttributeError):
164+
server.custom_views.populate_image(single_view, req_option)
165+
166+
119167
def test_populate_image_missing_id(server: TSC.Server) -> None:
120168
single_view = TSC.CustomViewItem()
121169
single_view._id = None

test/test_view.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,52 @@ 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_format_unsupported_version(server: TSC.Server) -> None:
272+
server.version = "3.28"
273+
response = POPULATE_PREVIEW_IMAGE.read_bytes()
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+
283+
with pytest.raises(UnsupportedAttributeError):
284+
server.views.populate_image(single_view, req_option)
285+
286+
241287
def test_populate_pdf(server: TSC.Server) -> None:
242288
response = POPULATE_PDF.read_bytes()
243289
with requests_mock.mock() as m:

0 commit comments

Comments
 (0)