mirror of
https://github.com/kennethreitz/tablib.git
synced 2026-06-05 06:56:13 +00:00
Implement feature that allows to export tabular data suited to a… (#437)
This commit is contained in:
committed by
Hugo van Kemenade
parent
4de2e17984
commit
fa30ea858d
+24
-1
@@ -7,6 +7,29 @@ Formats
|
||||
Tablib supports a wide variety of different tabular formats, both for input and
|
||||
output. Moreover, you can :ref:`register your own formats <newformats>`.
|
||||
|
||||
cli
|
||||
===
|
||||
|
||||
The ``cli`` format is currently export-only. The exports produce a representation
|
||||
table suited to a terminal.
|
||||
|
||||
When exporting to a CLI you can pass the table format with the ``tablefmt``
|
||||
parameter, the supported formats are::
|
||||
|
||||
>>> import tabulate
|
||||
>>> list(tabulate._table_formats)
|
||||
['simple', 'plain', 'grid', 'fancy_grid', 'github', 'pipe', 'orgtbl',
|
||||
'jira', 'presto', 'psql', 'rst', 'mediawiki', 'moinmoin', 'youtrack',
|
||||
'html', 'latex', 'latex_raw', 'latex_booktabs', 'tsv', 'textile']
|
||||
|
||||
For example::
|
||||
|
||||
dataset.export("cli", tablefmt="github")
|
||||
dataset.export("cli", tablefmt="grid")
|
||||
|
||||
This format is optional, install Tablib with ``pip install tablib[cli]`` to
|
||||
make the format available.
|
||||
|
||||
csv
|
||||
===
|
||||
|
||||
@@ -100,7 +123,7 @@ latex
|
||||
|
||||
Import/export using the LaTeX_ format. This format is export-only.
|
||||
If a title has been set, it will be exported as the table caption.
|
||||
|
||||
|
||||
.. _LaTeX: https://www.latex-project.org/
|
||||
|
||||
ods
|
||||
|
||||
@@ -33,7 +33,8 @@ setup(
|
||||
],
|
||||
python_requires='>=3.5',
|
||||
extras_require={
|
||||
'all': ['markuppy', 'odfpy', 'openpyxl>=2.4.0', 'pandas', 'pyyaml', 'xlrd', 'xlwt'],
|
||||
'all': ['markuppy', 'odfpy', 'openpyxl>=2.4.0', 'pandas', 'pyyaml', 'tabulate', 'xlrd', 'xlwt'],
|
||||
'cli': ['tabulate'],
|
||||
'html': ['markuppy'],
|
||||
'ods': ['odfpy'],
|
||||
'pandas': ['pandas'],
|
||||
|
||||
@@ -12,6 +12,10 @@ from ._json import JSONFormat
|
||||
from ._tsv import TSVFormat
|
||||
|
||||
uninstalled_format_messages = {
|
||||
'cli': (
|
||||
"The 'cli' format is not available. You may want to install the tabulate "
|
||||
"package (or `pip install tablib[cli]`)."
|
||||
),
|
||||
'df': (
|
||||
"The 'df' format is not available. You may want to install the pandas "
|
||||
"package (or `pip install tablib[pandas]`)."
|
||||
@@ -88,7 +92,7 @@ class Registry:
|
||||
def register(self, key, format_or_path):
|
||||
from tablib.core import Databook, Dataset
|
||||
|
||||
# Create Databook.<format> read or read/write properties
|
||||
# Create Databook.<format> read or read/write properties
|
||||
setattr(Databook, key, ImportExportBookDescriptor(key, format_or_path))
|
||||
|
||||
# Create Dataset.<format> read or read/write properties,
|
||||
@@ -124,6 +128,8 @@ class Registry:
|
||||
if find_spec('pandas'):
|
||||
self.register('df', 'tablib.formats._df.DataFrameFormat')
|
||||
self.register('rst', 'tablib.formats._rst.ReSTFormat')
|
||||
if find_spec('tabulate'):
|
||||
self.register('cli', 'tablib.formats._cli.CLIFormat')
|
||||
|
||||
def formats(self):
|
||||
for key, frm in self._formats.items():
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
"""Tablib - Command-line Interface table export support.
|
||||
|
||||
Generates a representation for CLI from the dataset.
|
||||
Wrapper for tabulate library.
|
||||
"""
|
||||
from tabulate import tabulate as Tabulate
|
||||
|
||||
|
||||
class CLIFormat:
|
||||
""" Class responsible to export to CLI Format """
|
||||
title = 'cli'
|
||||
DEFAULT_FMT = 'plain'
|
||||
|
||||
@classmethod
|
||||
def export_set(cls, dataset, **kwargs):
|
||||
"""Returns CLI representation of a Dataset."""
|
||||
if dataset.headers:
|
||||
kwargs.setdefault('headers', dataset.headers)
|
||||
kwargs.setdefault('tablefmt', cls.DEFAULT_FMT)
|
||||
return Tabulate(dataset, **kwargs)
|
||||
@@ -5,5 +5,6 @@ odfpy
|
||||
openpyxl>=2.4.0
|
||||
pandas
|
||||
pyyaml
|
||||
tabulate
|
||||
xlrd
|
||||
xlwt
|
||||
|
||||
@@ -1288,3 +1288,14 @@ class DocTests(unittest.TestCase):
|
||||
import tablib.formats._rst
|
||||
results = doctest.testmod(tablib.formats._rst)
|
||||
self.assertEqual(results.failed, 0)
|
||||
|
||||
|
||||
class CliTests(BaseTestCase):
|
||||
def test_cli_export_github(self):
|
||||
self.assertEqual('|---|---|---|\n| a | b | c |', tablib.Dataset(['a','b','c']).export('cli', tablefmt='github'))
|
||||
|
||||
def test_cli_export_simple(self):
|
||||
self.assertEqual('- - -\na b c\n- - -', tablib.Dataset(['a','b','c']).export('cli', tablefmt='simple'))
|
||||
|
||||
def test_cli_export_grid(self):
|
||||
self.assertEqual('+---+---+---+\n| a | b | c |\n+---+---+---+', tablib.Dataset(['a','b','c']).export('cli', tablefmt='grid'))
|
||||
|
||||
Reference in New Issue
Block a user