Skip to content

pipen.utils

module

pipen.utils

Provide some utilities

Classes
  • RichHandler Subclass of rich.logging.RichHandler, showing log levels as a singlecharacter </>
  • RichConsole A high level console interface.</>
Functions
  • 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</>
class

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)

Bases
rich.logging.RichHandler logging.Handler logging.Filterer

Subclass of rich.logging.RichHandler, showing log levels as a singlecharacter

Parameters
  • 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) If log_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 of RichHandler.KEYWORDS.
Methods
  • 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.</>
method

addFilter(filter)

Add the specified filter to this handler.

method

removeFilter(filter)

Remove the specified filter from this handler.

method

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.

method

createLock()

Acquire a thread lock for serializing access to the underlying I/O.

method

acquire()

Acquire the I/O thread lock.

method

release()

Release the I/O thread lock.

method

setLevel(level)

Set the logging level of this handler. level must be an int or a str.

method

format(record)

Format the specified record.

If a formatter is set, use it. Otherwise, use the default formatter for the module.

method

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.

method

setFormatter(fmt)

Set the formatter for this handler.

method

flush()

Ensure all logging output has been flushed.

This version does nothing and is intended to be implemented by subclasses.

method

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.

method

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.

method

emit(record)

Invoked by logging.

method

render_message(record, message)

Render message text in to Text.

Parameters
  • record (LogRecord) logging Record.
  • message (str) String containing log message.
Returns (ConsoleRenderable)

Renderable to display log message.

method

render(record, traceback, message_renderable)

Render log for display.

Parameters
  • record (LogRecord) logging Record.
  • traceback (Optional[Traceback]) Traceback instance or None for no Traceback.
  • message_renderable (ConsoleRenderable) Renderable (typically Text) containing log message contents.
Returns (ConsoleRenderable)

Renderable to display log.

method

get_level_text(record)

Get the level name from the record.

Parameters
  • record (LogRecord) LogRecord instance.
Returns (Text)

A tuple of the style and level name.

class

pipen.utils.RichConsole(*args, **kwargs)

Bases
rich.console.Console

A high level console interface.

Attributes
  • 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.</>
Methods
  • __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 of Segment 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.</>
method

set_live(live)

Set Live instance. Used by Live context manager.

Parameters
  • live (Live) Live instance using this Console.
Raises
  • errors.LiveError If this Console has a Live context currently active.
method

clear_live()

Clear the Live instance.

method

push_render_hook(hook)

Add a new render hook to the stack.

Parameters
  • hook (RenderHook) Render hook instance.
method

pop_render_hook()

Pop the last renderhook from the stack.

method

__enter__() → Console

Own context manager to enter buffer context.

method

__exit__(exc_type, exc_value, traceback)

Exit buffer context.

method

begin_capture()

Begin capturing console output. Call :meth:end_capture to exit capture mode and return output.

method

end_capture()

End capture mode and return captured string.

Returns (str)

Console output.

method

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.

Parameters
  • theme (Theme) A theme instance.
  • inherit (bool, optional) Inherit existing styles. Defaults to True.
method

pop_theme()

Remove theme from top of stack, restoring previous theme.

method

use_theme(theme, inherit=True)

Use a different theme for the duration of the context manager.

Parameters
  • theme (Theme) Theme instance to user.
  • inherit (bool, optional) Inherit existing console styles. Defaults to True.
Returns (ThemeContext)

[description]

method

bell()

Play a 'bell' sound (if supported by the terminal).

method

capture()

A context manager to capture the result of print() or log() in a string,rather than writing it to the console.

Example
>>> from rich.console import Console>>> console = Console()
>>> with console.capture() as capture:
...     console.print("[bold magenta]Hello World[/]")
>>> print(capture.get())
Returns (Capture)

Context manager with disables writing to the terminal.

method

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.

Parameters
  • 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.
Example
>>> from rich.console import Console>>> from rich.__main__ import make_test_card
>>> console = Console()
>>> with console.pager():
        console.print(make_test_card())
Returns (PagerContext)

A context manager.

method

line(count=1)

Write new line(s).

Parameters
  • count (int, optional) Number of new lines. Defaults to 1.
method

clear(home=True)

Clear the screen.

Parameters
  • home (bool, optional) Also move the cursor to 'home' position. Defaults to True.
method

status(status, spinner='dots', spinner_style='status.spinner', speed=1.0, refresh_per_second=12.5)

Display a status and spinner.

Parameters
  • 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.
Returns (Status)

A Status object that may be used as a context manager.

method

show_cursor(show=True) → bool

Show or hide the cursor.

Parameters
  • show (bool, optional) Set visibility of the cursor.
method

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.

Parameters
  • enable (bool, optional) Enable (True) or disable (False) alternate screen. Defaults to True.
Returns (bool)

True if the control codes were written.

method

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.

Parameters
  • title (str) The new title of the terminal window.
Returns (bool)

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.

method

screen(hide_cursor=True, style=None)

Context manager to enable and disable 'alternative screen' mode.

Parameters
  • hide_cursor (bool, optional) Also hide the cursor. Defaults to False.
  • style (Style, optional) Optional style for screen. Defaults to None.
Returns (~ScreenContext)

Context which enables alternate screen on enter, and disables it on exit.

method

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.

Parameters
  • renderable (RenderableType) Any renderable or string.
  • options (Optional[ConsoleOptions], optional) Options to use when measuring, or Noneto use default options. Defaults to None.
Returns (Measurement)

A measurement of the renderable.

generator

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.

Parameters
  • 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.
Returns (Iterable[Segment])

An iterable of segments that may be rendered.

method

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.
method

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.

Parameters
  • 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 to None.
  • overflow (str, optional) Overflow method: "crop", "fold", or "ellipsis". Defaults to None.
  • emoji (Optional[bool], optional) Enable emoji, or None to use Console default.
  • markup (Optional[bool], optional) Enable markup, or None to use Console default.
  • highlight (Optional[bool], optional) Enable highlighting, or None to use Console default.
  • highlighter (HighlighterType, optional) Optional highlighter to apply.
Returns (ConsoleRenderable)

Renderable object.

method

get_style(name, default=None)

Get a Style instance by its theme name or parse a definition.

Parameters
  • name (str) The name of a style or a style definition.
Returns (Style)

A Style object.

Raises
  • MissingStyle If no style could be parsed from name.
method

rule(title='', characters='─', style='rule.line', align='center')

Draw a line with optional centered title.

Parameters
  • 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".
method

control(*control)

Insert non-printing control codes.

method

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.

Parameters
  • 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, or None to useconsole default. Defaults to None.
method

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.

Parameters
  • 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 to None.
  • 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, or None to use console default. Defaults to None.
  • markup (Optional[bool], optional) Enable markup, or None to use console default. Defaults to None.
  • highlight (Optional[bool], optional) Enable automatic highlighting, or None to use console default. Defaults to None.
  • width (Optional[int], optional) Width of output, or None to auto-detect. Defaults to None.
  • 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 or None forConsole default. Defaults to None.
  • new_line_start (bool, False) Insert a new line at the start if the output contains more than one line. Defaults to False.
method

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.

Parameters
  • 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.
method

update_screen(renderable, region=None, options=None)

Update the screen at a given offset.

Parameters
  • renderable (RenderableType) A Rich renderable.
  • region (Region, optional) Region of screen to update, or None for entire screen. Defaults to None.
Raises
  • errors.NoAltScreen If the Console isn't in alt screen mode.
method

update_screen_lines(lines, x=0, y=0)

Update lines of the screen at a given offset.

Parameters
  • 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.
Raises
  • errors.NoAltScreen If the Console isn't in alt screen mode.
method

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.

Parameters
  • 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 traceback
  • word_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.
method

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.

Parameters
  • 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 to None.
  • emoji (Optional[bool], optional) Enable emoji code, or None to use console default. Defaults to None.
  • markup (Optional[bool], optional) Enable markup, or None to use console default. Defaults to None.
  • highlight (Optional[bool], optional) Enable automatic highlighting, or None to use console default. Defaults to None.
  • log_locals (bool, optional) Boolean to enable logging of locals where log()was called. Defaults to False.
  • _stack_offset (int, optional) Offset of caller from end of call stack. Defaults to 1.
method

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.

Parameters
  • 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.
Returns (str)

Text read from stdin.

method

export_text(clear=True, styles=False)

Generate text from console contents (requires record=True argument in constructor).

Parameters
  • clear (bool, optional) Clear record buffer after exporting. Defaults to True.
  • styles (bool, optional) If True, ansi escape codes will be included. False for plain text.Defaults to False.
Returns (str)

String containing console contents.

method

save_text(path, clear=True, styles=False)

Generate text from console and save to a given location (requires record=True argument in constructor).

Parameters
  • path (str) Path to write text files.
  • clear (bool, optional) Clear record buffer after exporting. Defaults to True.
  • styles (bool, optional) If True, ansi style codes will be included. False for plain text.Defaults to False.
method

export_html(theme=None, clear=True, code_format=None, inline_styles=False)

Generate HTML from console contents (requires record=True argument in constructor).

Parameters
  • theme (TerminalTheme, optional) TerminalTheme object containing console colors.
  • clear (bool, optional) Clear record buffer after exporting. Defaults to True.
  • code_format (str, optional) Format string to render HTML. In addition to '{foreground}','{background}', and '{code}', should contain '{stylesheet}' if inline_styles is False.
  • inline_styles (bool, optional) If True styles will be inlined in to spans, which makes fileslarger but easier to cut and paste markup. If False, styles will be embedded in a style tag. Defaults to False.
Returns (str)

String containing console contents as HTML.

method

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).

Parameters
  • path (str) Path to write html file.
  • theme (TerminalTheme, optional) TerminalTheme object containing console colors.
  • clear (bool, optional) Clear record buffer after exporting. Defaults to True.
  • code_format (str, optional) Format string to render HTML. In addition to '{foreground}','{background}', and '{code}', should contain '{stylesheet}' if inline_styles is False.
  • inline_styles (bool, optional) If True styles will be inlined in to spans, which makes fileslarger but easier to cut and paste markup. If False, styles will be embedded in a style tag. Defaults to False.
method

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).

Parameters
  • title (str, optional) The title of the tab in the output image
  • theme (TerminalTheme, optional) The TerminalTheme object to use to style the terminal
  • clear (bool, optional) Clear record buffer after exporting. Defaults to True
  • 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 the console.CONSOLE_SVG_FORMAT variable.
  • font_aspect_ratio (float, optional) The width to height ratio of the font used in the code_formatstring. 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 inside code_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.
method

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).

Parameters
  • path (str) The path to write the SVG to.
  • title (str, optional) The title of the tab in the output image
  • theme (TerminalTheme, optional) The TerminalTheme object to use to style the terminal
  • clear (bool, optional) Clear record buffer after exporting. Defaults to True
  • 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 the console.CONSOLE_SVG_FORMAT variable.
  • font_aspect_ratio (float, optional) The width to height ratio of the font used in the code_formatstring. 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 inside code_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.
function

pipen.utils.get_logger(name='core', level=None)

Get the logger by given plugin name

Parameters
  • level (str | int, optional) The initial level of the logger
Returns (LoggerAdapter)

The logger

function

pipen.utils.desc_from_docstring(obj, base)

Get the description from docstring

Only extract the summary.

Parameters
  • obj (Type[Pipen | Proc]) The object with docstring
Returns (str)

The summary as desc

function

pipen.utils.update_dict(parent, new, depth=0)

Update the new dict to the parent, but make sure parent does not change

Parameters
  • parent (Mapping) The parent dictionary
  • new (Mapping) The new dictionary
  • depth (int, optional) The depth to be copied. 0 for updating to the deepest level.
Examples
>>> parent = {"a": {"b": 1}}>>> new = {"a": {"c": 2}}
>>> update_dict(parent, new)
>>> # {"a": {"b": 1, "c": 2}}
Returns (Mapping)

The updated dictionary or None if both parent and new are None.

function

pipen.utils.strsplit(string, sep, maxsplit=-1, trim='both') → List

Split the string, with the ability to trim each part.

function

pipen.utils.get_shebang(script)

Get the shebang of the script

Parameters
  • script (str) The script string
Returns (str)

None if the script does not contain a shebang, otherwise the shebangwithout #! prefix

function

pipen.utils.ignore_firstline_dedent(text)

Like textwrap.dedent(), but ignore first empty lines

Parameters
  • text (str) The text the be dedented
Returns (str)

The dedented text

function

pipen.utils.copy_dict(dic, depth=1)

Deep copy a dict

Parameters
  • dic (Mapping) The dict to be copied
  • depth (int, optional) The depth to be deep copied
Returns (Mapping)

The deep-copied dict

function

pipen.utils.get_logpanel_width()

Get the width of the log content

Parameters
  • 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).
Returns (int)

The width of the log content

function

pipen.utils.log_rich_renderable(renderable, color, logfunc, *args, **kwargs)

Log a rich renderable to logger

Parameters
  • renderable (RenderableType) The rich renderable
  • logfunc (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 function
  • splitline Whether split the lines or log the entire message
function

pipen.utils.brief_list(blist)

Briefly show an integer list, combine the continuous numbers.

Parameters
  • blist (List) The list
Returns (str)

The string to show for the briefed list.

function

pipen.utils.pipen_banner()

The banner for pipen

Returns (RenderableType)

The banner renderable

function

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

Parameters
  • dir_depth (int, optional) The depth of the directory to check thelast modification time
Returns (float)

The last modification time of path

function

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

Parameters
  • obj (Any) The object to check
  • cls (type) The class to check
Returns (bool)

True if obj is a subclass of cls otherwise False

generator

pipen.utils.load_entrypoints(group)

Load objects from setuptools entrypoints by given group name

Parameters
  • group (str) The group name of the entrypoints
Returns (Iterable)

An iterable of tuples with name and the loaded object

function

pipen.utils.truncate_text(text, width, end='…')

Truncate a text not based on words/whitespacesOtherwise, we could use textwrap.shorten.

Parameters
  • text (str) The text to be truncated
  • width (int) The max width of the the truncated text
  • end (str, optional) The end string of the truncated text
Returns (str)

The truncated text with end appended.

function

pipen.utils.make_df_colnames_unique_inplace(thedf)

Make the columns of a data frame unique

Parameters
  • thedf (pandas.DataFrame) The data frame
function

pipen.utils.get_base(klass, abc_base, value, value_getter)

Get the base class where the value was first defined

Parameters
  • klass (Type) The class
  • abc_base (Type) The very base class to check in bases
  • value (Any) The value to check
  • value_getter (Callable) How to get the value from the class
Returns (Type)

The base class

function

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.

Parameters
  • **kwargs The kwargs to mark the proc
Returns (Callable)

The decorator

function

pipen.utils.get_marked(cls, mark_name, default=None)

Get the marked value from a proc

Parameters
  • cls (type) The proc
  • mark_name (str) The mark name
  • default (Any, optional) The default value if the mark is not found
Returns (Any)

The marked value

function

pipen.utils.is_valid_name(name)

Check if a name is valid for a proc or pipen

Parameters
  • name (str) The name to check
Returns (bool)

True if valid, otherwise False

function

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.

Parameters
  • obj (str | Type[Proc] | Type[ProcGroup] | Type[Pipen]) The Pipen, Proc or ProcGroup object. It can also be a string inthe format of part1: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 by getattr(module, part2), where module is loaded from part1.
  • 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
Returns (Pipen)

The loaded Pipen object

Raises
  • TypeError If obj or loaded obj is not a Pipen, Proc or ProcGroup
function

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().

Parameters
  • *flags (str) Additional flags to check in sys.argv (e.g. "-h", "--help")to determine if we are loading the pipeline
  • argv (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 if sys.argv[0] is "@pipen" or your_args contains some flags.
Returns (bool)

True if we are loading the pipeline (argv[0] == "@pipen"),otherwise False