pipen.utils
Provide some utilities
RichHandler
— Subclass of rich.logging.RichHandler, showing log levels as a singlecharacter </>RichConsole
— A high level console interface.</>
brief_list
(
blist
)
(str) — Briefly show an integer list, combine the continuous numbers.</>copy_dict
(
dic
,depth
)
(Mapping) — Deep copy a dict</>desc_from_docstring
(
obj
,base
)
(str) — Get the description from docstring</>get_base
(
klass
,abc_base
,value
,value_getter
)
(Type) — Get the base class where the value was first defined</>get_logger
(
name
,level
)
(LoggerAdapter) — Get the logger by given plugin name</>get_logpanel_width
(
)
(int) — Get the width of the log content</>get_marked
(
cls
,mark_name
,default
)
(Any) — Get the marked value from a proc</>get_mtime
(
path
,dir_depth
)
(float) — Get the modification time of a path.If path is a directory, try to get the last modification time of the contents in the directory at given dir_depth </>get_shebang
(
script
)
(str) — Get the shebang of the script</>ignore_firstline_dedent
(
text
)
(str) — Like textwrap.dedent(), but ignore first empty lines</>is_loading_pipeline
(
*flags
,argv
)
(bool) — Check if we are loading the pipeline. Works only whenargv0
is "@pipen" while loading the pipeline. </>is_subclass
(
obj
,cls
)
(bool) — Tell if obj is a subclass of clsDifferences with issubclass is that we don't raise Type error if obj is not a class </>is_valid_name
(
name
)
(bool) — Check if a name is valid for a proc or pipen</>load_entrypoints
(
group
)
(Iterable) — Load objects from setuptools entrypoints by given group name</>load_pipeline
(
obj
,argv0
,argv1p
,**kwargs
)
(Pipen) — Load a pipeline from a Pipen, Proc or ProcGroup object</>log_rich_renderable
(
renderable
,color
,logfunc
,*args
,**kwargs
)
— Log a rich renderable to logger</>make_df_colnames_unique_inplace
(
thedf
)
— Make the columns of a data frame unique</>mark
(
**kwargs
)
(Callable) — Mark a class (e.g. Proc) with given kwargs as metadata</>pipen_banner
(
)
(RenderableType) — The banner for pipen</>strsplit
(
string
,sep
,maxsplit
,trim
)
(List) — Split the string, with the ability to trim each part.</>truncate_text
(
text
,width
,end
)
(str) — Truncate a text not based on words/whitespacesOtherwise, we could use textwrap.shorten. </>update_dict
(
parent
,new
,depth
)
(Mapping) — Update the new dict to the parent, but make sure parent does not change</>
pipen.utils.
RichHandler
(
level=0
, console=None
, show_time=True
, omit_repeated_times=True
, show_level=True
, show_path=True
, enable_link_path=True
, highlighter=None
, markup=False
, rich_tracebacks=False
, tracebacks_width=None
, tracebacks_extra_lines=3
, tracebacks_theme=None
, tracebacks_word_wrap=True
, tracebacks_show_locals=False
, tracebacks_suppress=()
, locals_max_length=10
, locals_max_string=80
, log_time_format='[%x %X]'
, keywords=None
)
Subclass of rich.logging.RichHandler, showing log levels as a singlecharacter
level
(Union, optional) — Log level. Defaults to logging.NOTSET.show_time
(bool, optional) — Show a column for the time. Defaults to True.omit_repeated_times
(bool, optional) — Omit repetition of the same time. Defaults to True.show_level
(bool, optional) — Show a column for the level. Defaults to True.show_path
(bool, optional) — Show the path to the original log call. Defaults to True.enable_link_path
(bool, optional) — Enable terminal link of path column to file. Defaults to True.highlighter
(Optional, optional) — Highlighter to style log messages, or None to use ReprHighlighter. Defaults to None.markup
(bool, optional) — Enable console markup in log messages. Defaults to False.rich_tracebacks
(bool, optional) — Enable rich tracebacks with syntax highlighting and formatting. Defaults to False.tracebacks_width
(Optional, optional) — Number of characters used to render tracebacks, or None for full width. Defaults to None.tracebacks_extra_lines
(int, optional) — Additional lines of code to render tracebacks, or None for full width. Defaults to None.tracebacks_theme
(Optional, optional) — Override pygments theme used in traceback.tracebacks_word_wrap
(bool, optional) — Enable word wrapping of long tracebacks lines. Defaults to True.tracebacks_show_locals
(bool, optional) — Enable display of locals in tracebacks. Defaults to False.tracebacks_suppress
(Iterable, optional) — Optional sequence of modules or paths to exclude from traceback.locals_max_length
(int, optional) — Maximum length of containers before abbreviating, or None for no abbreviation.Defaults to 10.locals_max_string
(int, optional) — Maximum length of string before truncating, or None to disable. Defaults to 80.log_time_format
(Union, optional) — Iflog_time
is enabled, either string for strftime or callable that formats the time. Defaults to "[%x %X] ".keywords
(Optional, optional) — List of words to highlight instead ofRichHandler.KEYWORDS
.
acquire
(
)
— Acquire the I/O thread lock.</>addFilter
(
filter
)
— Add the specified filter to this handler.</>close
(
)
— Tidy up any resources used by the handler.</>createLock
(
)
— Acquire a thread lock for serializing access to the underlying I/O.</>emit
(
record
)
— Invoked by logging.</>filter
(
record
)
— Determine if a record is loggable by consulting all the filters.</>flush
(
)
— Ensure all logging output has been flushed.</>format
(
record
)
— Format the specified record.</>get_level_text
(
record
)
(Text) — Get the level name from the record.</>handle
(
record
)
— Conditionally emit the specified logging record.</>handleError
(
record
)
— Handle errors which occur during an emit() call.</>release
(
)
— Release the I/O thread lock.</>removeFilter
(
filter
)
— Remove the specified filter from this handler.</>render
(
record
,traceback
,message_renderable
)
(ConsoleRenderable) — Render log for display.</>render_message
(
record
,message
)
(ConsoleRenderable) — Render message text in to Text.</>setFormatter
(
fmt
)
— Set the formatter for this handler.</>setLevel
(
level
)
— Set the logging level of this handler. level must be an int or a str.</>
addFilter
(
filter
)
Add the specified filter to this handler.
removeFilter
(
filter
)
Remove the specified filter from this handler.
filter
(
record
)
Determine if a record is loggable by consulting all the filters.
The default is to allow the record to be logged; any filter can veto this and the record is then dropped. Returns a zero value if a record is to be dropped, else non-zero.
.. versionchanged:: 3.2
Allow filters to be just callables.
createLock
(
)
Acquire a thread lock for serializing access to the underlying I/O.
acquire
(
)
Acquire the I/O thread lock.
release
(
)
Release the I/O thread lock.
setLevel
(
level
)
Set the logging level of this handler. level must be an int or a str.
format
(
record
)
Format the specified record.
If a formatter is set, use it. Otherwise, use the default formatter for the module.
handle
(
record
)
Conditionally emit the specified logging record.
Emission depends on filters which may have been added to the handler. Wrap the actual emission of the record with acquisition/release of the I/O thread lock. Returns whether the filter passed the record for emission.
setFormatter
(
fmt
)
Set the formatter for this handler.
flush
(
)
Ensure all logging output has been flushed.
This version does nothing and is intended to be implemented by subclasses.
close
(
)
Tidy up any resources used by the handler.
This version removes the handler from an internal map of handlers, _handlers, which is used for handler lookup by name. Subclasses should ensure that this gets called from overridden close() methods.
handleError
(
record
)
Handle errors which occur during an emit() call.
This method should be called from handlers when an exception is encountered during an emit() call. If raiseExceptions is false, exceptions get silently ignored. This is what is mostly wanted for a logging system - most users will not care about errors in the logging system, they are more interested in application errors. You could, however, replace this with a custom handler if you wish. The record which was being processed is passed in to this method.
emit
(
record
)
Invoked by logging.
render_message
(
record
, message
)
Render message text in to Text.
record
(LogRecord) — logging Record.message
(str) — String containing log message.
Renderable to display log message.
render
(
record
, traceback
, message_renderable
)
Render log for display.
record
(LogRecord) — logging Record.traceback
(Optional[Traceback]) — Traceback instance or None for no Traceback.message_renderable
(ConsoleRenderable) — Renderable (typically Text) containing log message contents.
Renderable to display log.
get_level_text
(
record
)
Get the level name from the record.
record
(LogRecord) — LogRecord instance.
A tuple of the style and level name.
pipen.utils.
RichConsole
(
*args
, **kwargs
)
A high level console interface.
color_system
— Get color system string.</>encoding
— Get the encoding of the console file, e.g."utf-8"
.</>file
(IO) — Get the file object to write to.</>height
— Get the height of the console.</>is_alt_screen
— Check if the alt screen was enabled.</>is_dumb_terminal
— Detect dumb terminal.</>is_terminal
— Check if the console is writing to a terminal.</>options
(ConsoleOptions) — Get default console options.</>size
— Get the size of the console.</>width
— Get the width of the console.</>
__enter__
(
)
(Console) — Own context manager to enter buffer context.</>__exit__
(
exc_type
,exc_value
,traceback
)
— Exit buffer context.</>begin_capture
(
)
— Begin capturing console output. Call :meth:end_capture
to exit capture mode and return output.</>bell
(
)
— Play a 'bell' sound (if supported by the terminal).</>capture
(
)
(Capture) — A context manager to capture the result of print() or log() in a string,rather than writing it to the console. </>clear
(
home
)
— Clear the screen.</>clear_live
(
)
— Clear the Live instance.</>control
(
*control
)
— Insert non-printing control codes.</>end_capture
(
)
(str) — End capture mode and return captured string.</>export_html
(
theme
,clear
,code_format
,inline_styles
)
(str) — Generate HTML from console contents (requires record=True argument in constructor).</>export_svg
(
title
,theme
,clear
,code_format
,font_aspect_ratio
,unique_id
)
(str) — Generate an SVG from the console contents (requires record=True in Console constructor).</>export_text
(
clear
,styles
)
(str) — Generate text from console contents (requires record=True argument in constructor).</>get_style
(
name
,default
)
(Style) — Get a Style instance by its theme name or parse a definition.</>input
(
prompt
,markup
,emoji
,password
,stream
)
(str) — Displays a prompt and waits for input from the user. The prompt may contain color / style.</>line
(
count
)
— Write new line(s).</>log
(
*objects
,sep
,end
,style
,justify
,emoji
,markup
,highlight
,log_locals
,_stack_offset
)
— Log rich content to the terminal.</>measure
(
renderable
,options
)
(Measurement) — Measure a renderable. Returns a :class:~rich.measure.Measurement
object which containsinformation regarding the number of characters required to print the renderable. </>out
(
*objects
,sep
,end
,style
,highlight
)
— Output to the terminal. This is a low-level way of writing to the terminal which unlike:meth:~rich.console.Console.print
won't pretty print, wrap text, or apply markup, but will optionally apply highlighting and a basic style. </>pager
(
pager
,styles
,links
)
(PagerContext) — A context manager to display anything printed within a "pager". The pager applicationis defined by the system and will typically support at least pressing a key to scroll. </>pop_render_hook
(
)
— Pop the last renderhook from the stack.</>pop_theme
(
)
— Remove theme from top of stack, restoring previous theme.</>print
(
*objects
,sep
,end
,style
,justify
,overflow
,no_wrap
,emoji
,markup
,highlight
,width
,height
,crop
,soft_wrap
,new_line_start
)
— Print to the console.</>print_exception
(
width
,extra_lines
,theme
,word_wrap
,show_locals
,suppress
,max_frames
)
— Prints a rich render of the last exception and traceback.</>print_json
(
json
,data
,indent
,highlight
,skip_keys
,ensure_ascii
,check_circular
,allow_nan
,default
,sort_keys
)
— Pretty prints JSON. Output will be valid JSON.</>push_render_hook
(
hook
)
— Add a new render hook to the stack.</>push_theme
(
theme
,inherit
)
— Push a new theme on to the top of the stack, replacing the styles from the previous theme.Generally speaking, you should call :meth:~rich.console.Console.use_theme
to get a context manager, rather than calling this method directly. </>render
(
renderable
,options
)
(Iterable[Segment]) — Render an object in to an iterable ofSegment
instances.</>render_lines
(
renderable
,options
,style
,pad
,new_lines
)
(List) — Render objects in to a list of lines.</>render_str
(
text
,style
,justify
,overflow
,emoji
,markup
,highlight
,highlighter
)
(ConsoleRenderable) — Convert a string to a Text instance. This is called automatically ifyou print or log a string. </>rule
(
title
,characters
,style
,align
)
— Draw a line with optional centered title.</>save_html
(
path
,theme
,clear
,code_format
,inline_styles
)
— Generate HTML from console contents and write to a file (requires record=True argument in constructor).</>save_svg
(
path
,title
,theme
,clear
,code_format
,font_aspect_ratio
,unique_id
)
— Generate an SVG file from the console contents (requires record=True in Console constructor).</>save_text
(
path
,clear
,styles
)
— Generate text from console and save to a given location (requires record=True argument in constructor).</>screen
(
hide_cursor
,style
)
(~ScreenContext) — Context manager to enable and disable 'alternative screen' mode.</>set_alt_screen
(
enable
)
(bool) — Enables alternative screen mode.</>set_live
(
live
)
— Set Live instance. Used by Live context manager.</>set_window_title
(
title
)
(bool) — Set the title of the console terminal window.</>show_cursor
(
show
)
(bool) — Show or hide the cursor.</>status
(
status
,spinner
,spinner_style
,speed
,refresh_per_second
)
(Status) — Display a status and spinner.</>update_screen
(
renderable
,region
,options
)
— Update the screen at a given offset.</>update_screen_lines
(
lines
,x
,y
)
— Update lines of the screen at a given offset.</>use_theme
(
theme
,inherit
)
(ThemeContext) — Use a different theme for the duration of the context manager.</>
set_live
(
live
)
Set Live instance. Used by Live context manager.
live
(Live) — Live instance using this Console.
errors.LiveError
— If this Console has a Live context currently active.
clear_live
(
)
Clear the Live instance.
push_render_hook
(
hook
)
Add a new render hook to the stack.
hook
(RenderHook) — Render hook instance.
pop_render_hook
(
)
Pop the last renderhook from the stack.
__enter__
(
)
→ Console
Own context manager to enter buffer context.
__exit__
(
exc_type
, exc_value
, traceback
)
Exit buffer context.
begin_capture
(
)
Begin capturing console output. Call :meth:end_capture
to exit capture mode and return output.
end_capture
(
)
End capture mode and return captured string.
Console output.
push_theme
(
theme
, inherit=True
)
Push a new theme on to the top of the stack, replacing the styles from the previous theme.Generally speaking, you should call :meth:~rich.console.Console.use_theme
to get a context manager, rather
than calling this method directly.
theme
(Theme) — A theme instance.inherit
(bool, optional) — Inherit existing styles. Defaults to True.
pop_theme
(
)
Remove theme from top of stack, restoring previous theme.
use_theme
(
theme
, inherit=True
)
Use a different theme for the duration of the context manager.
theme
(Theme) — Theme instance to user.inherit
(bool, optional) — Inherit existing console styles. Defaults to True.
[description]
bell
(
)
Play a 'bell' sound (if supported by the terminal).
capture
(
)
A context manager to capture the result of print() or log() in a string,rather than writing it to the console.
>>> from rich.console import Console>>> console = Console()
>>> with console.capture() as capture:
... console.print("[bold magenta]Hello World[/]")
>>> print(capture.get())
Context manager with disables writing to the terminal.
pager
(
pager=None
, styles=False
, links=False
)
A context manager to display anything printed within a "pager". The pager applicationis defined by the system and will typically support at least pressing a key to scroll.
pager
(Pager, optional) — A pager object, or None to use :class:~rich.pager.SystemPager
. Defaults to None.styles
(bool, optional) — Show styles in pager. Defaults to False.links
(bool, optional) — Show links in pager. Defaults to False.
>>> from rich.console import Console>>> from rich.__main__ import make_test_card
>>> console = Console()
>>> with console.pager():
console.print(make_test_card())
A context manager.
line
(
count=1
)
Write new line(s).
count
(int, optional) — Number of new lines. Defaults to 1.
clear
(
home=True
)
Clear the screen.
home
(bool, optional) — Also move the cursor to 'home' position. Defaults to True.
status
(
status
, spinner='dots'
, spinner_style='status.spinner'
, speed=1.0
, refresh_per_second=12.5
)
Display a status and spinner.
status
(RenderableType) — A status renderable (str or Text typically).spinner
(str, optional) — Name of spinner animation (see python -m rich.spinner). Defaults to "dots".spinner_style
(StyleType, optional) — Style of spinner. Defaults to "status.spinner".speed
(float, optional) — Speed factor for spinner animation. Defaults to 1.0.refresh_per_second
(float, optional) — Number of refreshes per second. Defaults to 12.5.
A Status object that may be used as a context manager.
show_cursor
(
show=True
)
→ bool
Show or hide the cursor.
show
(bool, optional) — Set visibility of the cursor.
set_alt_screen
(
enable=True
)
Enables alternative screen mode.
Note, if you enable this mode, you should ensure that is disabled before
the application exits. See :meth:~rich.Console.screen
for a context manager
that handles this for you.
enable
(bool, optional) — Enable (True) or disable (False) alternate screen. Defaults to True.
True if the control codes were written.
set_window_title
(
title
)
Set the title of the console terminal window.
Warning: There is no means within Rich of "resetting" the window title to its previous value, meaning the title you set will persist even after your application exits.
fish
shell resets the window title before and after each command by default,
negating this issue. Windows Terminal and command prompt will also reset the title for you.
Most other shells and terminals, however, do not do this.
Some terminals may require configuration changes before you can set the title. Some terminals may not support setting the title at all.
Other software (including the terminal itself, the shell, custom prompts, plugins, etc.) may also set the terminal window title. This could result in whatever value you write using this method being overwritten.
title
(str) — The new title of the terminal window.
True if the control code to change the terminal title was written, otherwise False. Note that a return value of True does not guarantee that the window title has actually changed, since the feature may be unsupported/disabled in some terminals.
screen
(
hide_cursor=True
, style=None
)
Context manager to enable and disable 'alternative screen' mode.
hide_cursor
(bool, optional) — Also hide the cursor. Defaults to False.style
(Style, optional) — Optional style for screen. Defaults to None.
Context which enables alternate screen on enter, and disables it on exit.
measure
(
renderable
, options=None
)
Measure a renderable. Returns a :class:~rich.measure.Measurement
object which containsinformation regarding the number of characters required to print the renderable.
renderable
(RenderableType) — Any renderable or string.options
(Optional[ConsoleOptions], optional) — Options to use when measuring, or Noneto use default options. Defaults to None.
A measurement of the renderable.
render
(
renderable
, options=None
)
Render an object in to an iterable of Segment
instances.
This method contains the logic for rendering objects with the console protocol. You are unlikely to need to use it directly, unless you are extending the library.
renderable
(RenderableType) — An object supporting the console protocol, oran object that may be converted to a string.options
(ConsoleOptions, optional) — An options object, or None to use self.options. Defaults to None.
An iterable of segments that may be rendered.
render_lines
(
renderable
, options=None
, style=None
, pad=True
, new_lines=False
)
→ List
Render objects in to a list of lines.
The output of render_lines is useful when further formatting of rendered console text
is required, such as the Panel class which draws a border around any renderable object.
Args:
renderable (RenderableType): Any object renderable in the console.
options (Optional[ConsoleOptions], optional): Console options, or None to use self.options. Default to ``None``.
style (Style, optional): Optional style to apply to renderables. Defaults to ``None``.
pad (bool, optional): Pad lines shorter than render width. Defaults to ``True``.
new_lines (bool, optional): Include "
" characters at end of lines.
Returns:
List[List[Segment]]: A list of lines, where a line is a list of Segment objects.
render_str
(
text
, style=''
, justify=None
, overflow=None
, emoji=None
, markup=None
, highlight=None
, highlighter=None
)
Convert a string to a Text instance. This is called automatically ifyou print or log a string.
text
(str) — Text to render.style
(Union[str, Style], optional) — Style to apply to rendered text.justify
(str, optional) — Justify method: "default", "left", "center", "full", or "right". Defaults toNone
.overflow
(str, optional) — Overflow method: "crop", "fold", or "ellipsis". Defaults toNone
.emoji
(Optional[bool], optional) — Enable emoji, orNone
to use Console default.markup
(Optional[bool], optional) — Enable markup, orNone
to use Console default.highlight
(Optional[bool], optional) — Enable highlighting, orNone
to use Console default.highlighter
(HighlighterType, optional) — Optional highlighter to apply.
Renderable object.
get_style
(
name
, default=None
)
Get a Style instance by its theme name or parse a definition.
name
(str) — The name of a style or a style definition.
A Style object.
MissingStyle
— If no style could be parsed from name.
rule
(
title=''
, characters='─'
, style='rule.line'
, align='center'
)
Draw a line with optional centered title.
title
(str, optional) — Text to render over the rule. Defaults to "".characters
(str, optional) — Character(s) to form the line. Defaults to "─".style
(str, optional) — Style of line. Defaults to "rule.line".align
(str, optional) — How to align the title, one of "left", "center", or "right". Defaults to "center".
control
(
*control
)
Insert non-printing control codes.
out
(
*objects
, sep=' '
, end='\n'
, style=None
, highlight=None
)
Output to the terminal. This is a low-level way of writing to the terminal which unlike:meth:~rich.console.Console.print
won't pretty print, wrap text, or apply markup, but will
optionally apply highlighting and a basic style.
sep
(str, optional) — String to write between print data. Defaults to " ".end
(str, optional) — String to write at end of print data. Defaults to "\n".style
(Union[str, Style], optional) — A style to apply to output. Defaults to None.highlight
(Optional[bool], optional) — Enable automatic highlighting, orNone
to useconsole default. Defaults toNone
.
print
(
*objects
, sep=' '
, end='\n'
, style=None
, justify=None
, overflow=None
, no_wrap=None
, emoji=None
, markup=None
, highlight=None
, width=None
, height=None
, crop=True
, soft_wrap=None
, new_line_start=False
)
Print to the console.
sep
(str, optional) — String to write between print data. Defaults to " ".end
(str, optional) — String to write at end of print data. Defaults to "\n".style
(Union[str, Style], optional) — A style to apply to output. Defaults to None.justify
(str, optional) — Justify method: "default", "left", "right", "center", or "full". Defaults toNone
.overflow
(str, optional) — Overflow method: "ignore", "crop", "fold", or "ellipsis". Defaults to None.no_wrap
(Optional[bool], optional) — Disable word wrapping. Defaults to None.emoji
(Optional[bool], optional) — Enable emoji code, orNone
to use console default. Defaults toNone
.markup
(Optional[bool], optional) — Enable markup, orNone
to use console default. Defaults toNone
.highlight
(Optional[bool], optional) — Enable automatic highlighting, orNone
to use console default. Defaults toNone
.width
(Optional[int], optional) — Width of output, orNone
to auto-detect. Defaults toNone
.crop
(Optional[bool], optional) — Crop output to width of terminal. Defaults to True.soft_wrap
(bool, optional) — Enable soft wrap mode which disables word wrapping and cropping of text orNone
forConsole default. Defaults toNone
.new_line_start
(bool, False) — Insert a new line at the start if the output contains more than one line. Defaults toFalse
.
print_json
(
json=None
, data=None
, indent=2
, highlight=True
, skip_keys=False
, ensure_ascii=False
, check_circular=True
, allow_nan=True
, default=None
, sort_keys=False
)
Pretty prints JSON. Output will be valid JSON.
json
(Optional[str]) — A string containing JSON.data
(Any) — If json is not supplied, then encode this data.indent
(Union[None, int, str], optional) — Number of spaces to indent. Defaults to 2.highlight
(bool, optional) — Enable highlighting of output: Defaults to True.skip_keys
(bool, optional) — Skip keys not of a basic type. Defaults to False.ensure_ascii
(bool, optional) — Escape all non-ascii characters. Defaults to False.check_circular
(bool, optional) — Check for circular references. Defaults to True.allow_nan
(bool, optional) — Allow NaN and Infinity values. Defaults to True.default
(Callable, optional) — A callable that converts values that can not be encodedin to something that can be JSON encoded. Defaults to None.sort_keys
(bool, optional) — Sort dictionary keys. Defaults to False.
update_screen
(
renderable
, region=None
, options=None
)
Update the screen at a given offset.
renderable
(RenderableType) — A Rich renderable.region
(Region, optional) — Region of screen to update, or None for entire screen. Defaults to None.
errors.NoAltScreen
— If the Console isn't in alt screen mode.
update_screen_lines
(
lines
, x=0
, y=0
)
Update lines of the screen at a given offset.
lines
(List[List[Segment]]) — Rendered lines (as produced by :meth:~rich.Console.render_lines
).x
(int, optional) — x offset (column no). Defaults to 0.y
(int, optional) — y offset (column no). Defaults to 0.
errors.NoAltScreen
— If the Console isn't in alt screen mode.
print_exception
(
width=100
, extra_lines=3
, theme=None
, word_wrap=False
, show_locals=False
, suppress=()
, max_frames=100
)
Prints a rich render of the last exception and traceback.
width
(Optional[int], optional) — Number of characters used to render code. Defaults to 100.extra_lines
(int, optional) — Additional lines of code to render. Defaults to 3.theme
(str, optional) — Override pygments theme used in tracebackword_wrap
(bool, optional) — Enable word wrapping of long lines. Defaults to False.show_locals
(bool, optional) — Enable display of local variables. Defaults to False.suppress
(Iterable[Union[str, ModuleType]]) — Optional sequence of modules or paths to exclude from traceback.max_frames
(int) — Maximum number of frames to show in a traceback, 0 for no maximum. Defaults to 100.
log
(
*objects
, sep=' '
, end='\n'
, style=None
, justify=None
, emoji=None
, markup=None
, highlight=None
, log_locals=False
, _stack_offset=1
)
Log rich content to the terminal.
sep
(str, optional) — String to write between print data. Defaults to " ".end
(str, optional) — String to write at end of print data. Defaults to "\n".style
(Union[str, Style], optional) — A style to apply to output. Defaults to None.justify
(str, optional) — One of "left", "right", "center", or "full". Defaults toNone
.emoji
(Optional[bool], optional) — Enable emoji code, orNone
to use console default. Defaults to None.markup
(Optional[bool], optional) — Enable markup, orNone
to use console default. Defaults to None.highlight
(Optional[bool], optional) — Enable automatic highlighting, orNone
to use console default. Defaults to None.log_locals
(bool, optional) — Boolean to enable logging of locals wherelog()
was called. Defaults to False._stack_offset
(int, optional) — Offset of caller from end of call stack. Defaults to 1.
input
(
prompt=''
, markup=True
, emoji=True
, password=False
, stream=None
)
Displays a prompt and waits for input from the user. The prompt may contain color / style.
It works in the same way as Python's builtin :func:input
function and provides elaborate line editing and history features if Python's builtin :mod:readline
module is previously loaded.
prompt
(Union[str, Text]) — Text to render in the prompt.markup
(bool, optional) — Enable console markup (requires a str prompt). Defaults to True.emoji
(bool, optional) — Enable emoji (requires a str prompt). Defaults to True.password
(bool, optional) — (bool, optional): Hide typed text. Defaults to False.stream
(Optional, optional) — (TextIO, optional): Optional file to read input from (rather than stdin). Defaults to None.
Text read from stdin.
export_text
(
clear=True
, styles=False
)
Generate text from console contents (requires record=True argument in constructor).
clear
(bool, optional) — Clear record buffer after exporting. Defaults toTrue
.styles
(bool, optional) — IfTrue
, ansi escape codes will be included.False
for plain text.Defaults toFalse
.
String containing console contents.
save_text
(
path
, clear=True
, styles=False
)
Generate text from console and save to a given location (requires record=True argument in constructor).
path
(str) — Path to write text files.clear
(bool, optional) — Clear record buffer after exporting. Defaults toTrue
.styles
(bool, optional) — IfTrue
, ansi style codes will be included.False
for plain text.Defaults toFalse
.
export_html
(
theme=None
, clear=True
, code_format=None
, inline_styles=False
)
Generate HTML from console contents (requires record=True argument in constructor).
theme
(TerminalTheme, optional) — TerminalTheme object containing console colors.clear
(bool, optional) — Clear record buffer after exporting. Defaults toTrue
.code_format
(str, optional) — Format string to render HTML. In addition to '{foreground}','{background}', and '{code}', should contain '{stylesheet}' if inline_styles isFalse
.inline_styles
(bool, optional) — IfTrue
styles will be inlined in to spans, which makes fileslarger but easier to cut and paste markup. IfFalse
, styles will be embedded in a style tag. Defaults to False.
String containing console contents as HTML.
save_html
(
path
, theme=None
, clear=True
, code_format='<!DOCTYPE html>\n<html>\n<head>\n<meta charset="UTF-8">\n<style>\n{stylesheet}\nbody {{\n color: {foreground};\n background-color: {background};\n}}\n</style>\n</head>\n<body>\n <pre style="font-family:Menlo,\'DejaVu Sans Mono\',consolas,\'Courier New\',monospace"><code style="font-family:inherit">{code}</code></pre>\n</body>\n</html>\n'
, inline_styles=False
)
Generate HTML from console contents and write to a file (requires record=True argument in constructor).
path
(str) — Path to write html file.theme
(TerminalTheme, optional) — TerminalTheme object containing console colors.clear
(bool, optional) — Clear record buffer after exporting. Defaults toTrue
.code_format
(str, optional) — Format string to render HTML. In addition to '{foreground}','{background}', and '{code}', should contain '{stylesheet}' if inline_styles isFalse
.inline_styles
(bool, optional) — IfTrue
styles will be inlined in to spans, which makes fileslarger but easier to cut and paste markup. IfFalse
, styles will be embedded in a style tag. Defaults to False.
export_svg
(
title='Rich'
, theme=None
, clear=True
, code_format='<svg class="rich-terminal" viewBox="0 0 {width} {height}" xmlns="http://www.w3.org/2000/svg">\n <!-- Generated with Rich https://www.textualize.io -->\n <style>\n\n @font-face {{\n font-family: "Fira Code";\n src: local("FiraCode-Regular"),\n url("https://cdnjs.cloudflare.com/ajax/libs/firacode/6.2.0/woff2/FiraCode-Regular.woff2") format("woff2"),\n url("https://cdnjs.cloudflare.com/ajax/libs/firacode/6.2.0/woff/FiraCode-Regular.woff") format("woff");\n font-style: normal;\n font-weight: 400;\n }}\n @font-face {{\n font-family: "Fira Code";\n src: local("FiraCode-Bold"),\n url("https://cdnjs.cloudflare.com/ajax/libs/firacode/6.2.0/woff2/FiraCode-Bold.woff2") format("woff2"),\n url("https://cdnjs.cloudflare.com/ajax/libs/firacode/6.2.0/woff/FiraCode-Bold.woff") format("woff");\n font-style: bold;\n font-weight: 700;\n }}\n\n .{unique_id}-matrix {{\n font-family: Fira Code, monospace;\n font-size: {char_height}px;\n line-height: {line_height}px;\n font-variant-east-asian: full-width;\n }}\n\n .{unique_id}-title {{\n font-size: 18px;\n font-weight: bold;\n font-family: arial;\n }}\n\n {styles}\n </style>\n\n <defs>\n <clipPath id="{unique_id}-clip-terminal">\n <rect x="0" y="0" width="{terminal_width}" height="{terminal_height}" />\n </clipPath>\n {lines}\n </defs>\n\n {chrome}\n <g transform="translate({terminal_x}, {terminal_y})" clip-path="url(#{unique_id}-clip-terminal)">\n {backgrounds}\n <g class="{unique_id}-matrix">\n {matrix}\n </g>\n </g>\n</svg>\n'
, font_aspect_ratio=0.61
, unique_id=None
)
→ str
Generate an SVG from the console contents (requires record=True in Console constructor).
title
(str, optional) — The title of the tab in the output imagetheme
(TerminalTheme, optional) — TheTerminalTheme
object to use to style the terminalclear
(bool, optional) — Clear record buffer after exporting. Defaults toTrue
code_format
(str, optional) — Format string used to generate the SVG. Rich will inject a number of variablesinto the string in order to form the final SVG output. The default template used and the variables injected by Rich can be found by inspecting theconsole.CONSOLE_SVG_FORMAT
variable.font_aspect_ratio
(float, optional) — The width to height ratio of the font used in thecode_format
string. Defaults to 0.61, which is the width to height ratio of Fira Code (the default font). If you aren't specifying a different font insidecode_format
, you probably don't need this.unique_id
(str, optional) — unique id that is used as the prefix for various elements (CSS styles, nodeids). If not set, this defaults to a computed value based on the recorded content.
save_svg
(
path
, title='Rich'
, theme=None
, clear=True
, code_format='<svg class="rich-terminal" viewBox="0 0 {width} {height}" xmlns="http://www.w3.org/2000/svg">\n <!-- Generated with Rich https://www.textualize.io -->\n <style>\n\n @font-face {{\n font-family: "Fira Code";\n src: local("FiraCode-Regular"),\n url("https://cdnjs.cloudflare.com/ajax/libs/firacode/6.2.0/woff2/FiraCode-Regular.woff2") format("woff2"),\n url("https://cdnjs.cloudflare.com/ajax/libs/firacode/6.2.0/woff/FiraCode-Regular.woff") format("woff");\n font-style: normal;\n font-weight: 400;\n }}\n @font-face {{\n font-family: "Fira Code";\n src: local("FiraCode-Bold"),\n url("https://cdnjs.cloudflare.com/ajax/libs/firacode/6.2.0/woff2/FiraCode-Bold.woff2") format("woff2"),\n url("https://cdnjs.cloudflare.com/ajax/libs/firacode/6.2.0/woff/FiraCode-Bold.woff") format("woff");\n font-style: bold;\n font-weight: 700;\n }}\n\n .{unique_id}-matrix {{\n font-family: Fira Code, monospace;\n font-size: {char_height}px;\n line-height: {line_height}px;\n font-variant-east-asian: full-width;\n }}\n\n .{unique_id}-title {{\n font-size: 18px;\n font-weight: bold;\n font-family: arial;\n }}\n\n {styles}\n </style>\n\n <defs>\n <clipPath id="{unique_id}-clip-terminal">\n <rect x="0" y="0" width="{terminal_width}" height="{terminal_height}" />\n </clipPath>\n {lines}\n </defs>\n\n {chrome}\n <g transform="translate({terminal_x}, {terminal_y})" clip-path="url(#{unique_id}-clip-terminal)">\n {backgrounds}\n <g class="{unique_id}-matrix">\n {matrix}\n </g>\n </g>\n</svg>\n'
, font_aspect_ratio=0.61
, unique_id=None
)
Generate an SVG file from the console contents (requires record=True in Console constructor).
path
(str) — The path to write the SVG to.title
(str, optional) — The title of the tab in the output imagetheme
(TerminalTheme, optional) — TheTerminalTheme
object to use to style the terminalclear
(bool, optional) — Clear record buffer after exporting. Defaults toTrue
code_format
(str, optional) — Format string used to generate the SVG. Rich will inject a number of variablesinto the string in order to form the final SVG output. The default template used and the variables injected by Rich can be found by inspecting theconsole.CONSOLE_SVG_FORMAT
variable.font_aspect_ratio
(float, optional) — The width to height ratio of the font used in thecode_format
string. Defaults to 0.61, which is the width to height ratio of Fira Code (the default font). If you aren't specifying a different font insidecode_format
, you probably don't need this.unique_id
(str, optional) — unique id that is used as the prefix for various elements (CSS styles, nodeids). If not set, this defaults to a computed value based on the recorded content.
pipen.utils.
get_logger
(
name='core'
, level=None
)
Get the logger by given plugin name
level
(str | int, optional) — The initial level of the logger
The logger
pipen.utils.
desc_from_docstring
(
obj
, base
)
Get the description from docstring
Only extract the summary.
obj
(Type[Pipen | Proc]) — The object with docstring
The summary as desc
pipen.utils.
update_dict
(
parent
, new
, depth=0
)
Update the new dict to the parent, but make sure parent does not change
parent
(Mapping) — The parent dictionarynew
(Mapping) — The new dictionarydepth
(int, optional) — The depth to be copied. 0 for updating to the deepest level.
>>> parent = {"a": {"b": 1}}>>> new = {"a": {"c": 2}}
>>> update_dict(parent, new)
>>> # {"a": {"b": 1, "c": 2}}
The updated dictionary or None if both parent and new are None.
pipen.utils.
strsplit
(
string
, sep
, maxsplit=-1
, trim='both'
)
→ List
Split the string, with the ability to trim each part.
pipen.utils.
get_shebang
(
script
)
Get the shebang of the script
script
(str) — The script string
None if the script does not contain a shebang, otherwise the shebangwithout #!
prefix
pipen.utils.
ignore_firstline_dedent
(
text
)
Like textwrap.dedent(), but ignore first empty lines
text
(str) — The text the be dedented
The dedented text
pipen.utils.
copy_dict
(
dic
, depth=1
)
Deep copy a dict
dic
(Mapping) — The dict to be copieddepth
(int, optional) — The depth to be deep copied
The deep-copied dict
pipen.utils.
get_logpanel_width
(
)
Get the width of the log content
max_width
— The maximum width to returnNote that it's not the console width. With console width, you have to subtract the width of the log meta info (CONSOLE_WIDTH_SHIFT).
The width of the log content
pipen.utils.
log_rich_renderable
(
renderable
, color
, logfunc
, *args
, **kwargs
)
Log a rich renderable to logger
renderable
(RenderableType) — The rich renderablelogfunc
(Callable) — The log function, if message is not the first argument,use functools.partial to wrap it*args
(Any) — The arguments to the log function**kwargs
(Any) — The keyword arguments to the log functionsplitline
— Whether split the lines or log the entire message
pipen.utils.
brief_list
(
blist
)
Briefly show an integer list, combine the continuous numbers.
blist
(List) — The list
The string to show for the briefed list.
pipen.utils.
pipen_banner
(
)
The banner for pipen
The banner renderable
pipen.utils.
get_mtime
(
path
, dir_depth=1
)
Get the modification time of a path.If path is a directory, try to get the last modification time of the contents in the directory at given dir_depth
dir_depth
(int, optional) — The depth of the directory to check thelast modification time
The last modification time of path
pipen.utils.
is_subclass
(
obj
, cls
)
Tell if obj is a subclass of clsDifferences with issubclass is that we don't raise Type error if obj is not a class
obj
(Any) — The object to checkcls
(type) — The class to check
True if obj is a subclass of cls otherwise False
pipen.utils.
load_entrypoints
(
group
)
Load objects from setuptools entrypoints by given group name
group
(str) — The group name of the entrypoints
An iterable of tuples with name and the loaded object
pipen.utils.
truncate_text
(
text
, width
, end='…'
)
Truncate a text not based on words/whitespacesOtherwise, we could use textwrap.shorten.
text
(str) — The text to be truncatedwidth
(int) — The max width of the the truncated textend
(str, optional) — The end string of the truncated text
The truncated text with end appended.
pipen.utils.
make_df_colnames_unique_inplace
(
thedf
)
Make the columns of a data frame unique
thedf
(pandas.DataFrame) — The data frame
pipen.utils.
get_base
(
klass
, abc_base
, value
, value_getter
)
Get the base class where the value was first defined
klass
(Type) — The classabc_base
(Type) — The very base class to check in basesvalue
(Any) — The value to checkvalue_getter
(Callable) — How to get the value from the class
The base class
pipen.utils.
mark
(
**kwargs
)
Mark a class (e.g. Proc) with given kwargs as metadata
These marks will not be inherited by the subclasses if the class is
a subclass of Proc
or ProcGroup
.
**kwargs
— The kwargs to mark the proc
The decorator
pipen.utils.
get_marked
(
cls
, mark_name
, default=None
)
Get the marked value from a proc
cls
(type) — The procmark_name
(str) — The mark namedefault
(Any, optional) — The default value if the mark is not found
The marked value
pipen.utils.
is_valid_name
(
name
)
Check if a name is valid for a proc or pipen
name
(str) — The name to check
True if valid, otherwise False
pipen.utils.
load_pipeline
(
obj
, argv0=None
, argv1p=None
, **kwargs
)
Load a pipeline from a Pipen, Proc or ProcGroup object
It does not only load the Pipen object or convert the Proc/ProcGroup
object to Pipen, but also build the process relationships. So that we
can access pipeline.procs
and requires/nexts
of each proc.
To avoid running the pipeline and notify the plugins that this is just
for loading the pipeline, sys.argv[0]
is set to @pipen
.
obj
(str | Type[Proc] | Type[ProcGroup] | Type[Pipen]) — The Pipen, Proc or ProcGroup object. It can also be a string inthe format ofpart1:part2
to load the pipeline, where part1 is a path to a python file or package directory, and part2 is the name of the proc, procgroup or pipeline to load. It should be able to be loaded bygetattr(module, part2)
, where module is loaded frompart1
.argv0
(str | none, optional) — The value to replace sys.argv[0]. "@pipen" will be usedby default.argv1p
(Optional, optional) — The values to replace sys.argv[1:]. Do not replace by default.kwargs
— The kwargs to pass to the Pipen constructor
The loaded Pipen object
TypeError
— If obj or loaded obj is not a Pipen, Proc or ProcGroup
pipen.utils.
is_loading_pipeline
(
*flags
, argv=None
)
Check if we are loading the pipeline. Works only whenargv0
is "@pipen" while loading the pipeline.
Note if you are using this function at compile time, make
sure you load your pipeline using the string form (part1:part2
)
See more with load_pipline()
.
*flags
(str) — Additional flags to check in sys.argv (e.g. "-h", "--help")to determine if we are loading the pipelineargv
(Optional, optional) — The arguments to check. sys.argv is used by default.Note that the first argument should be included in the check. You could typically pass[sys.argv[0], *your_args]
to this if you want to check ifsys.argv[0]
is "@pipen" oryour_args
contains some flags.
True if we are loading the pipeline (argv[0] == "@pipen"),otherwise False