Merge pull request #222 from Lucas-C/format-markdown

New export format: markdown
pull/246/head
Vinayak Mehta 2021-06-28 00:35:56 +05:30 committed by GitHub
commit 56efcaa925
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 31 additions and 7 deletions

View File

@ -43,7 +43,7 @@ pass_config = click.make_pass_decorator(Config)
@click.option( @click.option(
"-f", "-f",
"--format", "--format",
type=click.Choice(["csv", "json", "excel", "html", "sqlite"]), type=click.Choice(["csv", "excel", "html", "json", "markdown", "sqlite"]),
help="Output file format.", help="Output file format.",
) )
@click.option("-z", "--zip", is_flag=True, help="Create ZIP archive.") @click.option("-z", "--zip", is_flag=True, help="Create ZIP archive.")

View File

@ -630,6 +630,21 @@ class Table(object):
with open(path, "w", encoding="utf-8") as f: with open(path, "w", encoding="utf-8") as f:
f.write(html_string) f.write(html_string)
def to_markdown(self, path, **kwargs):
"""Writes Table to a Markdown file.
For kwargs, check :meth:`pandas.DataFrame.to_markdown`.
Parameters
----------
path : str
Output filepath.
"""
md_string = self.df.to_markdown(**kwargs)
with open(path, "w", encoding="utf-8") as f:
f.write(md_string)
def to_sqlite(self, path, **kwargs): def to_sqlite(self, path, **kwargs):
"""Writes Table to sqlite database. """Writes Table to sqlite database.
@ -711,7 +726,7 @@ class TableList(object):
path : str path : str
Output filepath. Output filepath.
f : str f : str
File format. Can be csv, json, excel, html and sqlite. File format. Can be csv, excel, html, json, markdown or sqlite.
compress : bool compress : bool
Whether or not to add files to a ZIP archive. Whether or not to add files to a ZIP archive.
@ -724,7 +739,7 @@ class TableList(object):
kwargs = {"path": path, "dirname": dirname, "root": root, "ext": ext} kwargs = {"path": path, "dirname": dirname, "root": root, "ext": ext}
if f in ["csv", "json", "html"]: if f in ["csv", "html", "json", "markdown"]:
self._write_file(f=f, **kwargs) self._write_file(f=f, **kwargs)
if compress: if compress:
self._compress_dir(**kwargs) self._compress_dir(**kwargs)

View File

@ -21,6 +21,7 @@ requires = [
"pandas>=0.23.4", "pandas>=0.23.4",
"pdfminer.six>=20200726", "pdfminer.six>=20200726",
"PyPDF2>=1.26.0", "PyPDF2>=1.26.0",
"tabulate>=0.8.9",
] ]
cv_requires = ["opencv-python>=3.4.2.17"] cv_requires = ["opencv-python>=3.4.2.17"]

View File

@ -123,7 +123,7 @@ def test_cli_output_format():
cli, cli,
["--format", "json", "--output", outfile, "stream", infile], ["--format", "json", "--output", outfile, "stream", infile],
) )
assert result.exit_code == 0 assert result.exit_code == 0, f"Output: {result.output}"
# excel # excel
outfile = os.path.join(tempdir, "health.xlsx") outfile = os.path.join(tempdir, "health.xlsx")
@ -131,7 +131,7 @@ def test_cli_output_format():
cli, cli,
["--format", "excel", "--output", outfile, "stream", infile], ["--format", "excel", "--output", outfile, "stream", infile],
) )
assert result.exit_code == 0 assert result.exit_code == 0, f"Output: {result.output}"
# html # html
outfile = os.path.join(tempdir, "health.html") outfile = os.path.join(tempdir, "health.html")
@ -139,7 +139,15 @@ def test_cli_output_format():
cli, cli,
["--format", "html", "--output", outfile, "stream", infile], ["--format", "html", "--output", outfile, "stream", infile],
) )
assert result.exit_code == 0 assert result.exit_code == 0, f"Output: {result.output}"
# markdown
outfile = os.path.join(tempdir, "health.md")
result = runner.invoke(
cli,
["--format", "markdown", "--output", outfile, "stream", infile],
)
assert result.exit_code == 0, f"Output: {result.output}"
# zip # zip
outfile = os.path.join(tempdir, "health.csv") outfile = os.path.join(tempdir, "health.csv")
@ -155,7 +163,7 @@ def test_cli_output_format():
infile, infile,
], ],
) )
assert result.exit_code == 0 assert result.exit_code == 0, f"Output: {result.output}"
def test_cli_quiet(): def test_cli_quiet():