diff options
| author | Kenneth Reitz <me@kennethreitz.com> | 2011-01-31 01:35:52 -0500 |
|---|---|---|
| committer | Kenneth Reitz <me@kennethreitz.com> | 2011-01-31 01:35:52 -0500 |
| commit | 26b6faa88d0c272f7f8153cbd2de6ce451e22caf (patch) | |
| tree | ef25c2f5969cfa88d4802243a588e753a2118834 /tablib/formats | |
| parent | 7727171379683dcf859a344d6c8a43dc6d5cfa2b (diff) | |
| parent | 140736ff332ff164f18821ec150488b1a2092898 (diff) | |
| download | tablib-0.9.3.tar.gz | |
Merge branch 'release/0.9.3'v0.9.3
Diffstat (limited to 'tablib/formats')
| -rw-r--r-- | tablib/formats/__init__.py | 3 | ||||
| -rw-r--r-- | tablib/formats/_html.py | 53 | ||||
| -rw-r--r-- | tablib/formats/_json.py | 6 |
3 files changed, 58 insertions, 4 deletions
diff --git a/tablib/formats/__init__.py b/tablib/formats/__init__.py index f5960b8..147df31 100644 --- a/tablib/formats/__init__.py +++ b/tablib/formats/__init__.py @@ -8,5 +8,6 @@ import _json as json import _xls as xls import _yaml as yaml import _tsv as tsv +import _html as html -available = (json, xls, yaml, csv, tsv) +available = (json, xls, yaml, csv, tsv, html) diff --git a/tablib/formats/_html.py b/tablib/formats/_html.py new file mode 100644 index 0000000..13dc055 --- /dev/null +++ b/tablib/formats/_html.py @@ -0,0 +1,53 @@ +# -*- coding: utf-8 -*- + +""" Tablib - HTML export support. +""" + +from StringIO import StringIO + +from tablib.packages import markup +import tablib + +BOOK_ENDINGS = 'h3' + +title = 'html' +extentions = ('html', ) + + +def export_set(dataset): + """HTML representation of a Dataset.""" + + stream = StringIO() + + page = markup.page() + page.table.open() + + if dataset.headers is not None: + page.thead.open() + headers = markup.oneliner.th(dataset.headers) + page.tr(headers) + page.thead.close() + + for row in dataset: + html_row = markup.oneliner.td(row) + page.tr(html_row) + + page.table.close() + + stream.writelines(str(page)) + + return stream.getvalue() + + +def export_book(databook): + """HTML representation of a Databook.""" + + stream = StringIO() + + for i, dset in enumerate(databook._datasets): + title = (dset.title if dset.title else 'Set %s' % (i)) + stream.write('<%s>%s</%s>\n' % (BOOK_ENDINGS, title, BOOK_ENDINGS)) + stream.write(dset.html) + stream.write('\n') + + return stream.getvalue() diff --git a/tablib/formats/_json.py b/tablib/formats/_json.py index da31b23..7f31ee5 100644 --- a/tablib/formats/_json.py +++ b/tablib/formats/_json.py @@ -26,11 +26,11 @@ def export_set(dataset): def export_book(databook): """Returns JSON representation of Databook.""" return json.dumps(databook._package()) - + def import_set(dset, in_stream): """Returns dataset from JSON stream.""" - + dset.wipe() dset.dict = json.loads(in_stream) @@ -52,4 +52,4 @@ def detect(stream): json.loads(stream) return True except ValueError: - return False
\ No newline at end of file + return False |
