Skip to content

Text to Text

Overview

Text to text component allows you to take in user inputted text and return a transformed text. This is part of Mesop Labs.

Examples

import mesop as me
import mesop.labs as mel


def load(e: me.LoadEvent):
  me.set_theme_mode("system")


@me.page(
  on_load=load,
  security_policy=me.SecurityPolicy(
    allowed_iframe_parents=["https://google.github.io"]
  ),
  path="/text_to_text",
  title="Text to Text Example",
)
def app():
  mel.text_to_text(
    upper_case_stream,
    title="Text to Text Example",
  )


def upper_case_stream(s: str):
  return "Echo: " + s

API

text_to_text

Creates a simple UI which takes in a text input and returns a text output.

This function creates event handlers for text input and output operations using the provided transform function to process the input and generate the output.

PARAMETER DESCRIPTION
transform

Function that takes in a string input and either returns or yields a string output.

TYPE: Callable[[str], Generator[str, None, None] | str]

title

Headline text to display at the top of the UI

TYPE: str | None DEFAULT: None

transform_mode

Specifies how the output should be updated when yielding an output using a generator. - "append": Concatenates each new piece of text to the existing output. - "replace": Replaces the existing output with each new piece of text.

TYPE: Literal['append', 'replace'] DEFAULT: 'append'

text_io

Deprecated: Use text_to_text instead which provides the same functionality with better default settings.

This function creates event handlers for text input and output operations using the provided transform function to process the input and generate the output.

PARAMETER DESCRIPTION
transform

Function that takes in a string input and either returns or yields a string output.

TYPE: Callable[[str], Generator[str, None, None] | str]

title

Headline text to display at the top of the UI

TYPE: str | None DEFAULT: None

transform_mode

Specifies how the output should be updated when yielding an output using a generator. - "append": Concatenates each new piece of text to the existing output. - "replace": Replaces the existing output with each new piece of text.

TYPE: Literal['append', 'replace'] DEFAULT: 'replace'