From 998c7dbeaf8815abfe792024e6442b7fdfebc151 Mon Sep 17 00:00:00 2001 From: Suresh Kumar Date: Wed, 13 Oct 2021 22:15:43 +0530 Subject: [PATCH 1/6] added example for export --- examples/export.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 examples/export.py diff --git a/examples/export.py b/examples/export.py new file mode 100644 index 00000000..1e81c74d --- /dev/null +++ b/examples/export.py @@ -0,0 +1,44 @@ +""" +Demonstrates export console output +""" + +from rich.console import Console +from rich.table import Table + +console = Console(record=True) + + +def print_table(): + table = Table(title="Star Wars Movies") + + table.add_column("Released", style="cyan", no_wrap=True) + table.add_column("Title", style="magenta") + table.add_column("Box Office", justify="right", style="green") + + table.add_row("Dec 20, 2019", "Star Wars: The Rise of Skywalker", "$952,110,690") + table.add_row("May 25, 2018", "Solo: A Star Wars Story", "$393,151,347") + table.add_row("Dec 15, 2017", "Star Wars Ep. V111: The Last Jedi", "$1,332,539,889") + table.add_row("Dec 16, 2016", "Rogue One: A Star Wars Story", "$1,332,439,889") + + console.print(table, justify="center") + + +# Prints table +print_table() + +# Get console output as text +text = console.export_text() + +# Calling print_table again because export_text() +# by default console output buffer is flushed once export function is called +print_table() + +# Get console output as html +# use clear=False so output is not flushed after export +html = console.export_html(clear=False) + +# Export text output to table_export.txt +console.save_text("table_export.txt", clear=False) + +# Export html output to table_export.html +console.save_html("table_export.html") From 2eed45e62757e08f1305afaee028b254dbc156b0 Mon Sep 17 00:00:00 2001 From: Suresh Kumar Date: Wed, 13 Oct 2021 22:19:29 +0530 Subject: [PATCH 2/6] updated comments --- examples/export.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/export.py b/examples/export.py index 1e81c74d..77ac0fb8 100644 --- a/examples/export.py +++ b/examples/export.py @@ -29,8 +29,8 @@ print_table() # Get console output as text text = console.export_text() -# Calling print_table again because export_text() -# by default console output buffer is flushed once export function is called +# Calling print_table again because console output buffer +# is flushed once export function is called print_table() # Get console output as html From 0cf528d9a88e33e7ad72a53b13c1d0302c78e18d Mon Sep 17 00:00:00 2001 From: Suresh Kumar Date: Sat, 16 Oct 2021 19:04:34 +0530 Subject: [PATCH 3/6] added descriptive comments --- examples/export.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/examples/export.py b/examples/export.py index 77ac0fb8..d16daa86 100644 --- a/examples/export.py +++ b/examples/export.py @@ -24,21 +24,27 @@ def print_table(): # Prints table +print("print table to the console") print_table() # Get console output as text +print("geting console output as text") text = console.export_text() # Calling print_table again because console output buffer # is flushed once export function is called +print("print table to the console") print_table() # Get console output as html # use clear=False so output is not flushed after export +print("geting console output as html") html = console.export_html(clear=False) # Export text output to table_export.txt +print("exporting console output to table_export.txt") console.save_text("table_export.txt", clear=False) # Export html output to table_export.html +print("exporting console output to table_export.html") console.save_html("table_export.html") From ddde385389f7e4c4798491f142e0b8acfc7e4b23 Mon Sep 17 00:00:00 2001 From: Suresh Kumar Date: Sat, 16 Oct 2021 19:05:47 +0530 Subject: [PATCH 4/6] fix typo --- examples/export.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/export.py b/examples/export.py index d16daa86..07387bc4 100644 --- a/examples/export.py +++ b/examples/export.py @@ -28,7 +28,7 @@ print("print table to the console") print_table() # Get console output as text -print("geting console output as text") +print("getting console output as text") text = console.export_text() # Calling print_table again because console output buffer @@ -38,7 +38,7 @@ print_table() # Get console output as html # use clear=False so output is not flushed after export -print("geting console output as html") +print("getting console output as html") html = console.export_html(clear=False) # Export text output to table_export.txt From 8943924d007a6df67d85ccaa4ca9fc4ba69b7afb Mon Sep 17 00:00:00 2001 From: Suresh Kumar Date: Sat, 16 Oct 2021 19:16:10 +0530 Subject: [PATCH 5/6] export captured console output --- examples/export.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/examples/export.py b/examples/export.py index 07387bc4..a9792707 100644 --- a/examples/export.py +++ b/examples/export.py @@ -24,27 +24,25 @@ def print_table(): # Prints table -print("print table to the console") print_table() # Get console output as text -print("getting console output as text") text = console.export_text() +with open("plaintext_export.txt", "w") as file: + file.write(text) # Calling print_table again because console output buffer # is flushed once export function is called -print("print table to the console") print_table() # Get console output as html # use clear=False so output is not flushed after export -print("getting console output as html") html = console.export_html(clear=False) +with open("html_export.html", "w") as file: + file.write(html) # Export text output to table_export.txt -print("exporting console output to table_export.txt") -console.save_text("table_export.txt", clear=False) +console.save_text("rich_export.txt", clear=False) # Export html output to table_export.html -print("exporting console output to table_export.html") -console.save_html("table_export.html") +console.save_html("rich_export.html") From 5dddadb98340fec6afda80fd1a8ee1eda907b60a Mon Sep 17 00:00:00 2001 From: Suresh Kumar Date: Sat, 16 Oct 2021 22:04:45 +0530 Subject: [PATCH 6/6] print exports to terminal --- examples/export.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/examples/export.py b/examples/export.py index a9792707..dada493d 100644 --- a/examples/export.py +++ b/examples/export.py @@ -20,16 +20,18 @@ def print_table(): table.add_row("Dec 15, 2017", "Star Wars Ep. V111: The Last Jedi", "$1,332,539,889") table.add_row("Dec 16, 2016", "Rogue One: A Star Wars Story", "$1,332,439,889") - console.print(table, justify="center") + console.print(table) # Prints table print_table() # Get console output as text +file1 = "table_export_plaintext.txt" text = console.export_text() -with open("plaintext_export.txt", "w") as file: +with open(file1, "w") as file: file.write(text) +print(f"Exported console output as plain text to {file1}") # Calling print_table again because console output buffer # is flushed once export function is called @@ -37,12 +39,18 @@ print_table() # Get console output as html # use clear=False so output is not flushed after export +file2 = "table_export_html.html" html = console.export_html(clear=False) -with open("html_export.html", "w") as file: +with open(file2, "w") as file: file.write(html) +print(f"Exported console output as html to {file2}") # Export text output to table_export.txt -console.save_text("rich_export.txt", clear=False) +file3 = "table_export_plaintext2.txt" +console.save_text(file3, clear=False) +print(f"Exported console output as plain text to {file3}") # Export html output to table_export.html -console.save_html("rich_export.html") +file4 = "table_export_html2.html" +console.save_html(file4) +print(f"Exported console output as html to {file4}")