Skip to content

Commit

Permalink
Update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
thombashi committed Sep 24, 2023
1 parent 209395b commit 539bae3
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 18 deletions.
18 changes: 15 additions & 3 deletions docs/make_readme.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,24 @@ def write_examples(maker: ReadmeMaker) -> None:
maker.inc_indent_level()

maker.write_chapter("Write a Markdown table")
maker.write_file(examples_root.joinpath("table_format", "text", "markdown_example.txt"))
maker.write_file(examples_root.joinpath("table_format", "text", "markdown", "md_example.txt"))

with maker.indent():
maker.write_chapter("Write a Markdown table with a margin")
maker.write_chapter("Write a Markdown table with margins")
maker.write_file(
examples_root.joinpath("table_format", "text", "markdown_example_with_margin.txt")
examples_root.joinpath("table_format", "text", "markdown", "md_example_with_margin.txt")
)

maker.write_chapter("Write a GitHub Flavored Markdown (GFM) table")
maker.write_file(
examples_root.joinpath("table_format", "text", "markdown", "md_example_with_flavor.txt")
)

maker.write_chapter("Apply styles to GFM table with programmatically")
maker.write_file(
examples_root.joinpath(
"table_format", "text", "markdown", "md_example_with_style_filter.txt"
)
)

with maker.indent():
Expand Down
2 changes: 1 addition & 1 deletion docs/pages/examples/table_format/text/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Text formats
ltsv
html
json
markdown
markdown/markdown
mediawiki
rst/index
sourcecode/index
Expand Down
11 changes: 0 additions & 11 deletions docs/pages/examples/table_format/text/markdown.rst

This file was deleted.

19 changes: 19 additions & 0 deletions docs/pages/examples/table_format/text/markdown/markdown.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
.. _example-markdown-table-writer:

Markdown
----------------------------
|MarkdownTableWriter| class can write a table to the |stream| with Markdown table format from a data matrix.

.. include:: md_example.txt

Markdown with margins
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.. include:: md_example_with_margin.txt

GitHub flavored Markdown (GFM)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.. include:: md_example_with_flavor.txt

Apply styles to GFM table with programmatically
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.. include:: md_example_with_style_style.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
If you set ``flavor`` keyword argument of ``MarkdownTableWriter`` class to ``"github"`` or ``"gfm"``, the writer will output markdown tables with GitHub flavor.
GFM can apply some additional styles to tables such as ``fg_color`` (text color).

:Sample Code:
.. code-block:: python
:caption: Write a Markdown table with GitHub flavor

from pytablewriter import MarkdownTableWriter
from pytablewriter.style import Style

writer = MarkdownTableWriter(
column_styles=[
Style(fg_color="red"),
Style(fg_color="green", decoration_line="underline"),
],
headers=["A", "B"],
value_matrix=[
["abc", 1],
["efg", 2],
],
margin=1,
flavor="github",
enable_ansi_escape=False,
)
writer.write_table()

Rendered results can be found at `here <https://github.com/thombashi/pytablewriter/blob/master/docs/pages/examples/output/markdown/gfm.md>`__
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
:Sample Code:
.. code-block:: python
:caption: Write a Markdown table with a margin
:caption: Write a Markdown table with margins

from pytablewriter import MarkdownTableWriter

def main():
writer = MarkdownTableWriter(
table_name="write an example with a margin",
table_name="write a table with margins",
headers=["int", "float", "str", "bool", "mix", "time"],
value_matrix=[
[0, 0.1, "hoge", True, 0, "2017-01-01 03:04:05+0900"],
Expand All @@ -24,7 +24,7 @@
:Output:
.. code-block:: none

# write an example with a margin
# write a table with margins
| int | float | str | bool | mix | time |
| --: | ----: | ---- | ----- | -------: | ------------------------ |
| 0 | 0.10 | hoge | True | 0 | 2017-01-01 03:04:05+0900 |
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
You can apply styles to specific cells by using style filters.
Style filters will be written as functions.
Example of a style filter function are as follows:

:Sample Code:
.. code-block:: python
:caption: Write a Markdown table with GitHub flavor

from typing import Any, Optional

from pytablewriter import MarkdownTableWriter
from pytablewriter.style import Cell, Style


def style_filter(cell: Cell, **kwargs: Any) -> Optional[Style]:
if cell.is_header_row():
return None

if cell.col == 0:
return Style(font_weight="bold")

value = int(cell.value)

if value > 80:
return Style(fg_color="red", font_weight="bold", decoration_line="underline")
elif value > 50:
return Style(fg_color="yellow", font_weight="bold")
elif value > 20:
return Style(fg_color="green")

return Style(fg_color="lightblue")


writer = MarkdownTableWriter(
table_name="style filter example",
headers=["Key", "Value 1", "Value 2"],
value_matrix=[
["A", 95, 40],
["B", 55, 5],
["C", 30, 85],
["D", 0, 69],
],
flavor="github",
enable_ansi_escape=False,
)
writer.add_style_filter(style_filter)
writer.write_table()

Rendered results can be found at `here <https://github.com/thombashi/pytablewriter/blob/master/docs/pages/examples/output/markdown/style_filter.md>`__

0 comments on commit 539bae3

Please sign in to comment.