Skip to content

Commit

Permalink
Update README
Browse files Browse the repository at this point in the history
  • Loading branch information
thombashi committed Sep 24, 2023
1 parent 539bae3 commit 2e56aeb
Showing 1 changed file with 84 additions and 4 deletions.
88 changes: 84 additions & 4 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -159,16 +159,16 @@ Write a Markdown table

Rendered markdown at GitHub

Write a Markdown table with a margin
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Write a Markdown table with margins
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
:Sample Code:
.. code-block:: python
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 @@ -186,7 +186,7 @@ Write a Markdown table with a margin
:Output:
.. code-block::
# 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 All @@ -196,6 +196,86 @@ Write a Markdown table with a margin
``margin`` attribute can be available for all of the text format writer classes.

Write a GitHub Flavored Markdown (GFM) table
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
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
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>`__

Apply styles to GFM table with programmatically
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
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
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>`__

Write a Markdown table to a stream or a file
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
`Refer an example <https://github.com/thombashi/pytablewriter/blob/master/examples/py/stream/configure_stream.py>`__
Expand Down

0 comments on commit 2e56aeb

Please sign in to comment.