output
Provide access to output window and its functionality.
This module provides access to the output window for the currently running
pyRevit command. The proper way to access this wrapper object is through
the :func:get_output of :mod:pyrevit.script module. This method, in return
uses the pyrevit.output module to get access to the output wrapper.
Examples:
Here is the source of :func:pyrevit.script.get_output. As you can see this
functions calls the :func:pyrevit.output.get_output to receive the
output wrapper.
.. literalinclude:: ../../pyrevitlib/pyrevit/script.py :pyobject: get_output
Attributes
mlogger = logger.get_logger(__name__)
module-attribute
DEFAULT_STYLESHEET_NAME = 'outputstyles.css'
module-attribute
active_stylesheet = user_config.output_stylesheet or get_default_stylesheet()
module-attribute
Classes
PyRevitOutputWindow()
Bases: object
Wrapper to interact with the output window.
Source code in pyrevitlib/pyrevit/output/__init__.py
Attributes
window
property
PyRevitLabs.PyRevit.Runtime.ScriptConsole: Return output window object.
renderer
property
Return html renderer inside output window.
Returns:
| Type | Description |
|---|---|
WebBrowser
|
HTML renderer |
output_id
property
str: Return id of the output window.
In current implementation, Id of output window is equal to the unique id of the pyRevit command it belongs to. This means that all output windows belonging to the same pyRevit command, will have identical output_id values.
output_uniqueid
property
str: Return unique id of the output window.
In current implementation, unique id of output window is a GUID string generated when the output window is opened. This id is unique to the instance of output window.
is_closed_by_user
property
Whether the window has been closed by the user.
last_line
property
Last line of the output window.
debug_mode
property
writable
Set debug mode on output window and stream.
This will cause the output window to print information about the buffer stream and other aspects of the output window mechanism.
Functions
self_destruct(seconds)
Set self-destruct (close window) timer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seconds
|
int
|
number of seconds after which window is closed. |
required |
inject_to_head(element_tag, element_contents, attribs=None)
Inject html element to current html head of the output window.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
element_tag
|
str
|
html tag of the element e.g. 'div' |
required |
element_contents
|
str
|
html code of the element contents |
required |
attribs
|
obj: |
None
|
Examples:
output = pyrevit.output.get_output()
output.inject_to_head('script',
'', # no script since it's a link
{'src': js_script_file_path})
Source code in pyrevitlib/pyrevit/output/__init__.py
inject_to_body(element_tag, element_contents, attribs=None)
Inject html element to current html body of the output window.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
element_tag
|
str
|
html tag of the element e.g. 'div' |
required |
element_contents
|
str
|
html code of the element contents |
required |
attribs
|
obj: |
None
|
Examples:
output = pyrevit.output.get_output()
output.inject_to_body('script',
'', # no script since it's a link
{'src': js_script_file_path})
Source code in pyrevitlib/pyrevit/output/__init__.py
inject_script(script_code, attribs=None, body=False)
Inject script tag into current head (or body) of the output window.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
script_code
|
str
|
javascript code |
required |
attribs
|
obj: |
None
|
|
body
|
bool
|
injects script into body instead of head |
False
|
Examples:
output = pyrevit.output.get_output()
output.inject_script('', # no script since it's a link
{'src': js_script_file_path})
Source code in pyrevitlib/pyrevit/output/__init__.py
add_style(style_code, attribs=None)
Inject style tag into current html head of the output window.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
style_code
|
str
|
css styling code |
required |
attribs
|
obj: |
None
|
Examples:
Source code in pyrevitlib/pyrevit/output/__init__.py
get_head_html()
set_title(new_title)
set_width(width)
set_height(height)
set_font(font_family, font_size)
Set window font family to the new font family and size.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
font_family
|
str
|
font family name e.g. 'Courier New' |
required |
font_size
|
int
|
font size e.g. 16 |
required |
Source code in pyrevitlib/pyrevit/output/__init__.py
resize(width, height)
center()
Center the output window on the screen.
Source code in pyrevitlib/pyrevit/output/__init__.py
get_title()
get_width()
get_height()
close()
close_others(all_open_outputs=False)
Close all other windows that belong to the current command.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
all_open_outputs
|
bool
|
Close all any other windows if True |
False
|
Source code in pyrevitlib/pyrevit/output/__init__.py
hide()
show()
lock_size()
unlock_size()
freeze()
unfreeze()
save_contents(dest_file)
Save html code of the window.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dest_file
|
str
|
full path of the destination html file |
required |
Source code in pyrevitlib/pyrevit/output/__init__.py
open_url(dest_url)
Open url page in output window.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dest_url
|
str
|
web url of the target page |
required |
open_page(dest_file)
Open html page in output window.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dest_file
|
str
|
full path of the target html file |
required |
update_progress(cur_value, max_value)
Activate and update the output window progress bar.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cur_value
|
float
|
current progress value e.g. 50 |
required |
max_value
|
float
|
total value e.g. 100 |
required |
Examples:
Source code in pyrevitlib/pyrevit/output/__init__.py
reset_progress()
hide_progress()
unhide_progress()
indeterminate_progress(state)
show_logpanel()
hide_logpanel()
log_debug(message)
log_success(message)
log_info(message)
log_warning(message)
log_error(message)
set_icon(iconpath)
reset_icon()
print_html(html_str)
staticmethod
Add the html code to the output window.
Examples:
Source code in pyrevitlib/pyrevit/output/__init__.py
print_code(code_str)
staticmethod
Print code to the output window with special formatting.
Examples:
Source code in pyrevitlib/pyrevit/output/__init__.py
print_md(md_str)
staticmethod
Process markdown code and print to output window.
Examples:
Source code in pyrevitlib/pyrevit/output/__init__.py
print_table(table_data, columns=None, formats=None, title='', last_line_style='')
Print provided data in a table in output window.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table_data
|
list[iterable[Any]]
|
2D array of data |
required |
title
|
str
|
table title |
''
|
columns
|
list[str]
|
list of column names |
None
|
formats
|
list[str]
|
column data formats |
None
|
last_line_style
|
str
|
css style of last row |
''
|
Examples:
data = [
['row1', 'data', 'data', 80 ],
['row2', 'data', 'data', 45 ],
]
output.print_table(
table_data=data,
title="Example Table",
columns=["Row Name", "Column 1", "Column 2", "Percentage"],
formats=['', '', '', '{}%'],
last_line_style='color:red;'
)
Source code in pyrevitlib/pyrevit/output/__init__.py
table_html_header(columns, table_uid, border_style)
Helper method for print_html_table() method
Return html Parameters: list of column names a unique ID for this table's CSS classes CSS border style string for table cells Returns: HTML string containing the table header row Examples: Helper method for print_html_table() method Check that the table_data is present and is a list
Check that table_data rows are of the same length
Check that all print_html_table() kwargs of type list are of correct length Parameters: The whole table data as 2D list list of column names list of format strings for each column list of additional argument lists Returns: (bool, str) - (True/False, message) indicating result Examples: Print provided data in a HTML table in output window.
The same window can output several tables, each with their own formatting options. Parameters: 2D array of data table title list of column names column data formats using python string formatting css style of last row of data (NB applies to all tables in this output) list css align-text styles for header row list css align-text styles for data rows list of CSS widths in either px or % CSS compact border definition CSS to use for width for the whole table, in either px or % Repeat the header row at the table foot (useful for long tables) False to override the default white-grey row stripes and make all white) Examples: Prints given image to the output. Examples: Add hidden next page tag to the output window. This is helpful to silently separate the output to multiple pages
for better printing. Create clickable link for the provided element ids. This method, creates the link but does not print it directly. Parameters: single or multiple ids tile of the link. defaults to list of element ids Returns: clickable link Examples: :obj: :obj: :obj: :obj: :obj: :obj: :obj: :obj: Setup document closing event listener. Set active css stylesheet used by output window. Parameters: full path to stylesheet file row for the table header
Name
Type
Description
Default
columns
list[str]
required
table_uid
str
required
border_style
str
required
Name Type
Description
str
output = pyrevit.output.get_output()
# Basic usage - called internally by print_html_table()
columns = ["Name", "Age", "City"]
table_uid = 1
border_style = "border: 1px solid black;"
header_html = output.table_html_header(
columns, table_uid, border_style)
# Returns: "<thead><tr style='border: 1px solid black;'>" # "<th class='head_title-1-0' align='left'>Name</th>" # "<th class='head_title-1-1' align='left'>Age</th>" # "<th class='head_title-1-2' align='left'>City</th>" # "</tr></thead>"
# Without border style
header_html = output.table_html_header(
columns, table_uid, "")
# Returns: "<thead><tr>" # "<th class='head_title-1-0' align='left'>Name</th>" # "<th class='head_title-1-1' align='left'>Age</th>" # "<th class='head_title-1-2' align='left'>City</th>" # "</tr></thead>"
Source code in
pyrevitlib/pyrevit/output/__init__.py
table_check_input_lists(table_data, columns, formats, input_kwargs)
Name
Type
Description
Default
table_data
list[list[Any]]
required
columns
list[str]
required
formats
list[str]
required
input_kwargs
list[list[Any]]
required
Name Type
Description
tuple
output = pyrevit.output.get_output()
# Valid table data
table_data = [["John", 25, "NYC"], ["Jane", 30, "LA"]]
columns = ["Name", "Age", "City"]
formats = ["", "{} years", ""]
input_kwargs = [["left", "center", "right"],
["100px", "80px", "120px"]]
is_valid, message = output.table_check_input_lists(
table_data, columns, formats, input_kwargs)
# Returns: (True, "Inputs OK")
# Invalid - mismatched column count
table_data = [["John", 25], ["Jane", 30, "LA"]] # Inconsistent
is_valid, message = output.table_check_input_lists(
table_data, columns, formats, input_kwargs)
# Returns: (False, "Not all rows of table_data are of "
# "equal length")
# Invalid - wrong number of columns
columns = ["Name", "Age"] # Only 2 columns but data has 3
is_valid, message = output.table_check_input_lists(
table_data, columns, formats, input_kwargs)
# Returns: (False, "Column head list length not equal "
# "to data row")
# Invalid - empty table data
is_valid, message = output.table_check_input_lists(
[], columns, formats, input_kwargs)
# Returns: (False, "No table_data list")
Source code in
pyrevitlib/pyrevit/output/__init__.py681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
def table_check_input_lists(self,
table_data,
columns,
formats,
input_kwargs):
"""Helper method for print_html_table() method
Check that the table_data is present and is a list
Check that table_data rows are of the same length
Check that all print_html_table() kwargs of type list are of correct length
Args:
table_data (list[list[Any]]): The whole table data as 2D list
columns (list[str]): list of column names
formats (list[str]): list of format strings for each column
input_kwargs (list[list[Any]]): list of additional argument lists
Returns:
tuple: (bool, str) - (True/False, message) indicating result
Examples:
```python
output = pyrevit.output.get_output()
# Valid table data
table_data = [["John", 25, "NYC"], ["Jane", 30, "LA"]]
columns = ["Name", "Age", "City"]
formats = ["", "{} years", ""]
input_kwargs = [["left", "center", "right"],
["100px", "80px", "120px"]]
is_valid, message = output.table_check_input_lists(
table_data, columns, formats, input_kwargs)
# Returns: (True, "Inputs OK")
# Invalid - mismatched column count
table_data = [["John", 25], ["Jane", 30, "LA"]] # Inconsistent
is_valid, message = output.table_check_input_lists(
table_data, columns, formats, input_kwargs)
# Returns: (False, "Not all rows of table_data are of "
# "equal length")
# Invalid - wrong number of columns
columns = ["Name", "Age"] # Only 2 columns but data has 3
is_valid, message = output.table_check_input_lists(
table_data, columns, formats, input_kwargs)
# Returns: (False, "Column head list length not equal "
# "to data row")
# Invalid - empty table data
is_valid, message = output.table_check_input_lists(
[], columns, formats, input_kwargs)
# Returns: (False, "No table_data list")
```
"""
# First check positional and named keyword args
if not table_data:
return False, "No table_data list"
if not isinstance(table_data, list):
return False, "table_data is not a list"
# table_data is a list. The first sublist must also be a list
first_data_row = list(table_data[0])
if not isinstance(first_data_row, list):
return False, "table_data's first row is not a list or tuple ({})".format(type(first_data_row))
len_data_row = len(first_data_row)
if not all(len(row) == len_data_row for row in table_data):
return False, "Not all rows of table_data are of equal length"
if columns and len_data_row != len(columns): # columns is allowed to be None
return False, "Column head list length not equal to data row"
if formats and len_data_row != len(formats): # formats is allowed to be None
return False, "Formats list length not equal to data row"
# Next check **kwargs
# Loop through the lists and return if not a list or len not equal
for kwarg_list in input_kwargs:
if not kwarg_list: # No kwarg is OK beacause they are optional
continue
if not isinstance(kwarg_list, list):
return False, "One of the print_html_table kwargs that should be a list is not a list ({})".format(kwarg_list)
if len(kwarg_list) != len_data_row:
return False, "print_html_table kwarg list length problem (should match {} columns)".format(len_data_row)
return True, "Inputs OK"
print_html_table(table_data, columns=None, formats=None, title='', last_line_style='', **kwargs)
Name
Type
Description
Default
table_data
list[iterable[Any]]
required
title
str
''
columns
list[str]
None
formats
list[str]
None
last_line_style
str
''
column_head_align_styles
list[str]
required
column_data_align_styles
list[str]
required
column_widths
list[str]
required
column_vertical_border_style
str
required
table_width_style
str
required
repeat_head_as_foot
bool
required
row_striping
bool
required
data = [
['row1', 'data', 'data', 80 ],
['row2', 'data', 'data', 45 ],
]
output.print_html_table(
table_data=data,
title="Example Table",
columns=["Row Name", "Column 1", "Column 2", "Percentage"],
formats=['', '', '', '{}%'],
last_line_style='color:red;',
col_head_align_styles = ["left", "left", "center", "right"],
col_data_align_styles = ["left", "left", "center", "right"],
column_widths = ["100px", "100px", "500px", "100px"],
column_vertical_border_style = "border:black solid 1px",
table_width_style='width:100%',
repeat_head_as_foot=True,
row_striping=False
)
Returns:
Directly prints:
print_md of the title (str):
print_html of the generated HTML table with formatting.
Source code in
pyrevitlib/pyrevit/output/__init__.py769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
def print_html_table(self,
table_data,
columns=None,
formats=None,
title='',
last_line_style='',
**kwargs):
"""Print provided data in a HTML table in output window.
The same window can output several tables, each with their own formatting options.
Args:
table_data (list[iterable[Any]]): 2D array of data
title (str): table title
columns (list[str]): list of column names
formats (list[str]): column data formats using python string formatting
last_line_style (str): css style of last row of data (NB applies to all tables in this output)
column_head_align_styles (list[str]): list css align-text styles for header row
column_data_align_styles (list[str]): list css align-text styles for data rows
column_widths (list[str]): list of CSS widths in either px or %
column_vertical_border_style (str): CSS compact border definition
table_width_style (str): CSS to use for width for the whole table, in either px or %
repeat_head_as_foot (bool): Repeat the header row at the table foot (useful for long tables)
row_striping (bool): False to override the default white-grey row stripes and make all white)
Examples:
```python
data = [
['row1', 'data', 'data', 80 ],
['row2', 'data', 'data', 45 ],
]
output.print_html_table(
table_data=data,
title="Example Table",
columns=["Row Name", "Column 1", "Column 2", "Percentage"],
formats=['', '', '', '{}%'],
last_line_style='color:red;',
col_head_align_styles = ["left", "left", "center", "right"],
col_data_align_styles = ["left", "left", "center", "right"],
column_widths = ["100px", "100px", "500px", "100px"],
column_vertical_border_style = "border:black solid 1px",
table_width_style='width:100%',
repeat_head_as_foot=True,
row_striping=False
)
Returns:
Directly prints:
print_md of the title (str):
print_html of the generated HTML table with formatting.
```
"""
# Get user formatting options from kwargs
column_head_align_styles = kwargs.get("column_head_align_styles", None)
column_data_align_styles = kwargs.get("column_data_align_styles", None)
column_widths = kwargs.get("column_widths", None)
column_vertical_border_style = kwargs.get("column_vertical_border_style", None)
table_width_style = kwargs.get("table_width_style", None)
repeat_head_as_foot = kwargs.get("repeat_head_as_foot", False)
row_striping = kwargs.get("row_striping", True)
# Get a unique ID for each table from _table_counter
# This is used in HTML tags to define CSS classes for formatting per table
self._table_counter += 1
table_uid = self._table_counter
# Validate input arguments should be lists:
input_kwargs = [column_head_align_styles, column_data_align_styles, column_widths]
inputs_ok, inputs_msg = self.table_check_input_lists(table_data,
columns,
formats,
input_kwargs)
if not inputs_ok:
self.print_md('### :warning: {} '.format(inputs_msg))
return
if not row_striping:
# Override default original pyRevit white-grey row striping. Makes all rows white.
self.add_style('tr.data-row-{} {{ {style} }}'.format(table_uid, style="background-color: #ffffff"))
if last_line_style:
# Adds a CCS class to allow a last-row format per table (if several in the same output)
self.add_style('tr.data-row-{}:last-child {{ {style} }}'.format(table_uid, style=last_line_style))
if column_head_align_styles:
for i, s in enumerate(column_head_align_styles):
self.add_style('.head_title-{}-{} {{ text-align:{style} }}'.format(table_uid, i, style=s))
if column_data_align_styles:
for i, s in enumerate(column_data_align_styles):
self.add_style('.data_cell-{}-{} {{ text-align:{style} }}'.format(table_uid, i, style=s))
if table_width_style:
self.add_style('.tab-{} {{ width:{} }}'.format(table_uid, table_width_style))
# Open HTML table and its CSS class
html = "<table class='tab-{}'>".format(table_uid)
# Build colgroup if user argument specifies column widths
if column_widths:
COL = "<col style='width: {}'>"
html += '<colgroup>'
for w in column_widths:
html += COL.format(w)
html += "</colgroup>"
if column_vertical_border_style:
border_style = "style='{}'".format(column_vertical_border_style)
else:
border_style = ""
# Build header row (column titles) if requested
if columns:
html_head = self.table_html_header(columns, table_uid, border_style)
html += html_head
else:
html_head =""
repeat_head_as_foot = False
# Build body rows from 2D python list table_data
html += "<tbody>"
for row in table_data:
# Build an HTML data row with CSS class for this table
html += "<tr class='data-row-{}'>".format(table_uid)
for i, cell_data in enumerate(row):
# Slight workaround to be backwards compatible with pyRevit original print_table
# pyRevit documentation gives the example: formats=['', '', '', '{}%'],
# Sometimes giving an empty string, sometimes a placeholder with string formatting
if formats: # If format options provided
format_placeholder = formats[i] if formats[i] else "{}"
cell_data = format_placeholder.format(cell_data) # Insert data with or without formatting
html += "<td class='data_cell-{}-{}' {}>{}</td>".format(table_uid, i, border_style, cell_data)
html += "</tr>"
# Re-insert header row at the end, if requested and if column titles provided
if repeat_head_as_foot:
html += html_head
html += "</tbody>"
html += "</table>" # Close table
if title:
self.print_md('### {title}'.format(title=title))
self.print_html(html)
print_image(image_path)
Source code in
pyrevitlib/pyrevit/output/__init__.py
insert_divider(level='')
next_page()
Source code in
pyrevitlib/pyrevit/output/__init__.py
linkify(element_ids, title=None)
staticmethod
Name
Type
Description
Default
element_ids
ElementId | list[ElementId]
required
title
str
None
Type
Description
str
output = pyrevit.output.get_output()
for idx, elid in enumerate(element_ids):
print('{}: {}'.format(idx+1, output.linkify(elid)))
Source code in
pyrevitlib/pyrevit/output/__init__.py
make_chart(version=None)
make_line_chart(version=None)
PyRevitOutputChart: Return line chart object.
make_stacked_chart(version=None)
PyRevitOutputChart: Return stacked chart object.Source code in
pyrevitlib/pyrevit/output/__init__.py
make_bar_chart(version=None)
PyRevitOutputChart: Return bar chart object.
make_radar_chart(version=None)
PyRevitOutputChart: Return radar chart object.
make_polar_chart(version=None)
PyRevitOutputChart: Return polar chart object.
make_pie_chart(version=None)
PyRevitOutputChart: Return pie chart object.
make_doughnut_chart(version=None)
PyRevitOutputChart: Return dougnut chart object.
make_bubble_chart(version=None)
PyRevitOutputChart: Return bubble chart object.Functions
docclosing_eventhandler(sender, args)
setup_output_closer()
set_stylesheet(stylesheet)
Name
Type
Description
Default
stylesheet
str
required
Source code in
pyrevitlib/pyrevit/output/__init__.py
get_stylesheet()
get_default_stylesheet()
reset_stylesheet()
get_output()