Skip to content

Commit 202acea

Browse files
author
Russell Hay
authored
tableau#72 Add description to the field object (tableau#73)
* Add description to the field object Pull in the description if it exists on a `<column>` tag. This change returns the entire `<desc>` tag including all subtags. e.g.: ``` <desc> <formatted-text> <run>Total number of people in a country</run> </formatted-text> </desc> ``` * improving the test code to use 'in'
1 parent 8b8f351 commit 202acea

File tree

4 files changed

+31
-1
lines changed

4 files changed

+31
-1
lines changed

Examples/GetFields/show_fields.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
if field.default_aggregation:
2424
print(' the default aggregation is {}'.format(field.default_aggregation))
2525
blank_line = True
26+
if field.description:
27+
print(' the description is {}'.format(field.description))
2628

2729
if blank_line:
2830
print('')

tableaudocumentapi/field.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import functools
2+
import xml.etree.ElementTree as ET
3+
24

35
_ATTRIBUTES = [
46
'id', # Name of the field as specified in the file, usually surrounded by [ ]
@@ -8,6 +10,7 @@
810
'type', # three possible values: quantitative, ordinal, or nominal
911
'alias', # Name of the field as displayed in Tableau if the default name isn't wanted
1012
'calculation', # If this field is a calculated field, this will be the formula
13+
'description', # If this field has a description, this will be the description (including formatting tags)
1114
]
1215

1316
_METADATA_ATTRIBUTES = [
@@ -164,6 +167,11 @@ def default_aggregation(self):
164167
""" The default type of aggregation on the field (e.g Sum, Avg)"""
165168
return self._aggregation
166169

170+
@property
171+
def description(self):
172+
""" The contents of the <desc> tag on a field """
173+
return self._description
174+
167175
@property
168176
def worksheets(self):
169177
return list(self._worksheets)
@@ -184,3 +192,11 @@ def _read_calculation(xmldata):
184192
return None
185193

186194
return calc.attrib.get('formula', None)
195+
196+
@staticmethod
197+
def _read_description(xmldata):
198+
description = xmldata.find('.//desc')
199+
if description is None:
200+
return None
201+
202+
return u'{}'.format(ET.tostring(description, encoding='utf-8')) # This is necessary for py3 support

test/assets/datasource_test.tds

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,14 @@
7575
<column datatype='integer' name='[Number of Records]' role='measure' type='quantitative' user:auto-column='numrec'>
7676
<calculation class='tableau' formula='1' />
7777
</column>
78-
<column caption='A' datatype='string' name='[a]' role='dimension' type='nominal' />
78+
<column caption='A' datatype='string' name='[a]' role='dimension' type='nominal'>
79+
<desc>
80+
<formatted-text>
81+
<run bold='true' fontsize='96'>A thing</run>
82+
<run fontcolor='#686868'>&#10;Something will go here too, in a muted gray</run>
83+
</formatted-text>
84+
</desc>
85+
</column>
7986
<column caption='Today&apos;s Date' datatype='string' name='[Today&apos;s Date]' role='dimension' type='nominal' />
8087
<column caption='X' datatype='integer' name='[x]' role='measure' type='ordinal' />
8188
<column caption='Y' datatype='integer' name='[y]' role='measure' type='quantitative' />

test/test_datasource.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ def test_datasource_field_datatype(self):
5858
def test_datasource_field_role(self):
5959
self.assertEqual(self.ds.fields['[x]'].role, 'measure')
6060

61+
def test_datasource_field_description(self):
62+
actual = self.ds.fields['[a]'].description
63+
self.assertIsNotNone(actual)
64+
self.assertTrue(u'muted gray' in actual)
65+
6166

6267
class DataSourceFieldsTWB(unittest.TestCase):
6368

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy