{ "cells": [ { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "from textwrap import dedent\n", "\n", "import pytablewriter\n", "\n", "table_name = \"example_table\"\n", "headers = [\"int\", \"float\", \"str\", \"bool\", \"mix\", \"time\"]\n", "data = [\n", " [0, 0.1, \"hoge\", True, 0, \"2017-01-01 03:04:05+0900\"],\n", " [2, \"-2.23\", \"foo\", False, None, \"2017-12-23 12:34:51+0900\"],\n", " [3, 0, \"bar\", \"true\", \"inf\", \"2017-03-03 22:44:55+0900\"],\n", " [-10, -9.9, \"\", \"FALSE\", \"nan\", \"2017-01-01 00:00:00+0900\"],\n", "]" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "adoc\n", "asciidoc\n", "bold_unicode\n", "borderless\n", "css\n", "csv\n", "elasticsearch\n", "excel\n", "htm\n", "html\n", "javascript\n", "js\n", "json\n", "json_lines\n", "jsonl\n", "latex_matrix\n", "latex_table\n", "ldjson\n", "ltsv\n", "markdown\n", "md\n", "mediawiki\n", "ndjson\n", "null\n", "numpy\n", "pandas\n", "pandas_pickle\n", "py\n", "python\n", "rst\n", "rst_csv\n", "rst_csv_table\n", "rst_grid\n", "rst_grid_table\n", "rst_simple\n", "rst_simple_table\n", "space_aligned\n", "sqlite\n", "ssv\n", "toml\n", "tsv\n", "unicode\n", "yaml\n" ] } ], "source": [ "for name in pytablewriter.TableWriterFactory.get_format_names():\n", " print(name)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "adoc\n", "asc\n", "asciidoc\n", "css\n", "csv\n", "htm\n", "html\n", "js\n", "json\n", "jsonl\n", "ldjson\n", "ltsv\n", "md\n", "ndjson\n", "py\n", "rst\n", "sqlite\n", "sqlite3\n", "tex\n", "toml\n", "tsv\n", "xls\n", "xlsx\n", "yml\n" ] } ], "source": [ "for name in pytablewriter.TableWriterFactory.get_extensions():\n", " print(name)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "# zone\n", "|zone_id|country_code| zone_name |\n", "|------:|------------|----------------|\n", "| 1|AD |Europe/Andorra |\n", "| 2|AE |Asia/Dubai |\n", "| 3|AF |Asia/Kabul |\n", "| 4|AG |America/Antigua |\n", "| 5|AI |America/Anguilla|\n" ] } ], "source": [ "from pytablewriter import MarkdownTableWriter\n", "\n", "writer = MarkdownTableWriter()\n", "writer.table_name = \"zone\"\n", "writer.headers = [\"zone_id\", \"country_code\", \"zone_name\"]\n", "writer.value_matrix = [\n", " [\"1\", \"AD\", \"Europe/Andorra\"],\n", " [\"2\", \"AE\", \"Asia/Dubai\"],\n", " [\"3\", \"AF\", \"Asia/Kabul\"],\n", " [\"4\", \"AG\", \"America/Antigua\"],\n", " [\"5\", \"AI\", \"America/Anguilla\"],\n", "]\n", "\n", "writer.write_table()" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "# Markdown table with GitHub flavor\n", "| A | B |\n", "| --------------------------------- | --------------------------------------------: |\n", "| $$\\textcolor{#cd3131}{\\tt{abc}}$$ | $$\\textcolor{#0dbc79}{\\underline{\\tt{1}}}$$ |\n", "| $$\\textcolor{#cd3131}{\\tt{efg}}$$ | $$\\textcolor{#0dbc79}{\\underline{\\tt{2}}}$$ |\n" ] } ], "source": [ "from pytablewriter import MarkdownTableWriter\n", "from pytablewriter.style import Style\n", "\n", "writer = MarkdownTableWriter(\n", " table_name=\"Markdown table with GitHub flavor\",\n", " column_styles=[\n", " Style(fg_color=\"red\"),\n", " Style(fg_color=\"green\", decoration_line=\"underline\"),\n", " ],\n", " headers=[\"A\", \"B\"],\n", " value_matrix=[\n", " [\"abc\", 1],\n", " [\"efg\", 2],\n", " ],\n", " margin=1,\n", " flavor=\"github\",\n", " enable_ansi_escape=False,\n", ")\n", "writer.write_table()" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "# style filter example\n", "|Key|Value 1|Value 2|\n", "|---|------:|------:|\n", "|**A**|$$\\textcolor{#cd3131}{\\underline{\\tt{\\bf{95}}}}$$|$$\\textcolor{#0dbc79}{\\tt{40}}$$|\n", "|**B**|$$\\textcolor{#e5e510}{\\tt{\\bf{55}}}$$|$$\\textcolor{#3b8eea}{\\tt{5}}$$|\n", "|**C**|$$\\textcolor{#0dbc79}{\\tt{30}}$$|$$\\textcolor{#cd3131}{\\underline{\\tt{\\bf{85}}}}$$|\n", "|**D**|$$\\textcolor{#3b8eea}{\\tt{0}}$$|$$\\textcolor{#e5e510}{\\tt{\\bf{69}}}$$|\n" ] } ], "source": [ "from typing import Any, Optional\n", "\n", "from pytablewriter import MarkdownTableWriter\n", "from pytablewriter.style import Cell, Style\n", "\n", "\n", "def style_filter(cell: Cell, **kwargs: Any) -> Optional[Style]:\n", " if cell.is_header_row():\n", " return None\n", "\n", " if cell.col == 0:\n", " return Style(font_weight=\"bold\")\n", "\n", " value = int(cell.value)\n", "\n", " if value > 80:\n", " return Style(fg_color=\"red\", font_weight=\"bold\", decoration_line=\"underline\")\n", " elif value > 50:\n", " return Style(fg_color=\"yellow\", font_weight=\"bold\")\n", " elif value > 20:\n", " return Style(fg_color=\"green\")\n", "\n", " return Style(fg_color=\"lightblue\")\n", "\n", "\n", "writer = MarkdownTableWriter(\n", " table_name=\"style filter example\",\n", " headers=[\"Key\", \"Value 1\", \"Value 2\"],\n", " value_matrix=[\n", " [\"A\", 95, 40],\n", " [\"B\", 55, 5],\n", " [\"C\", 30, 85],\n", " [\"D\", 0, 69],\n", " ],\n", " flavor=\"github\",\n", " enable_ansi_escape=False,\n", ")\n", "writer.add_style_filter(style_filter)\n", "writer.write_table()" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\"int\",\"float\",\"str\",\"bool\",\"mix\",\"time\"\n", "0,0.1,\"hoge\",True,0,\"2017-01-01 03:04:05+0900\"\n", "2,-2.23,\"foo\",False,,\"2017-12-23 12:34:51+0900\"\n", "3,0,\"bar\",True,Infinity,\"2017-03-03 22:44:55+0900\"\n", "-10,-9.9,,False,NaN,\"2017-01-01 00:00:00+0900\"\n" ] } ], "source": [ "writer = pytablewriter.CsvTableWriter()\n", "writer.headers = headers\n", "writer.value_matrix = data\n", "\n", "writer.write_table()" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND \n", "32866 root 20 0 48344 3924 3448 R 5.6 0.2 0:00.03 top \n", " 1 root 20 0 212080 7676 5876 S 0.0 0.4 1:06.56 systemd \n", " 2 root 20 0 0 0 0 S 0.0 0.0 0:01.92 kthreadd \n", " 4 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kworker/0:0H\n" ] } ], "source": [ "writer = pytablewriter.SpaceAlignedTableWriter()\n", "writer.headers = [\n", " \"PID\",\n", " \"USER\",\n", " \"PR\",\n", " \"NI\",\n", " \"VIRT\",\n", " \"RES\",\n", " \"SHR\",\n", " \"S\",\n", " \"%CPU\",\n", " \"%MEM\",\n", " \"TIME+\",\n", " \"COMMAND\",\n", "]\n", "writer.value_matrix = csv1 = [\n", " [32866, \"root\", 20, 0, 48344, 3924, 3448, \"R\", 5.6, 0.2, \"0:00.03\", \"top\"],\n", " [1, \"root\", 20, 0, 212080, 7676, 5876, \"S\", 0, 0.4, \"1:06.56\", \"systemd\"],\n", " [2, \"root\", 20, 0, 0, 0, 0, \"S\", 0, 0, \"0:01.92\", \"kthreadd\"],\n", " [4, \"root\", 0, -20, 0, 0, 0, \"S\", 0, 0, \"0:00.00\", \"kworker/0:0H\"],\n", "]\n", "\n", "writer.write_table()" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\"int\"\t\"float\"\t\"str\"\t\"bool\"\t\"mix\"\t\"time\"\n", "0\t0.1\t\"hoge\"\tTrue\t0\t\"2017-01-01 03:04:05+0900\"\n", "2\t-2.23\t\"foo\"\tFalse\t\t\"2017-12-23 12:34:51+0900\"\n", "3\t0\t\"bar\"\tTrue\tInfinity\t\"2017-03-03 22:44:55+0900\"\n", "-10\t-9.9\t\tFalse\tNaN\t\"2017-01-01 00:00:00+0900\"\n" ] } ], "source": [ "writer = pytablewriter.TsvTableWriter()\n", "writer.headers = headers\n", "writer.value_matrix = data\n", "\n", "writer.write_table()" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "
int | \n", "float | \n", "str | \n", "bool | \n", "mix | \n", "time | \n", "
---|---|---|---|---|---|
0 | \n", "0.10 | \n", "hoge | \n", "True | \n", "0 | \n", "2017-01-01 03:04:05+0900 | \n", "
2 | \n", "-2.23 | \n", "foo | \n", "False | \n", "\n", " | 2017-12-23 12:34:51+0900 | \n", "
3 | \n", "0.00 | \n", "bar | \n", "True | \n", "Infinity | \n", "2017-03-03 22:44:55+0900 | \n", "
-10 | \n", "-9.90 | \n", "\n", " | False | \n", "NaN | \n", "2017-01-01 00:00:00+0900 | \n", "