Formatting and color the text

Hello, Dash community,

I have a textbox that allows users to enter any statement. The text will be shown after triggering the confirm button. The default color is black, how can I show the text with different colors? Such as “You have entered” as blue and “Game of Thrones” as red?

For example,

to this,

import numpy as np
import pandas as pd
import dash
from dash.dependencies import Input, Output, State
import dash_html_components as html
import dash_core_components as dcc
import dash_bootstrap_components as dbc
import dash_table as dt

external_stylesheets = ['']

app = dash.Dash(__name__,

app.layout = html.Div(children=[

    # Header
        html.H3(children=["This is my title"],
                style={"text-align": "center"})

    # Textarea
        placeholder='Please Enter a Description...',
        style={'width': '100%', 'height': 200},

    # Button
    html.Button('Confirm', id='button', n_clicks=0),


    # children

            type="circle",  # circle default
        style={'width': '100%', 'padding': '5px 5px', 'display': 'inline-block'}),


    [Output(component_id='output_children', component_property='children')],
    [Input(component_id='button', component_property='n_clicks'),
     State(component_id='textarea', component_property='value')],
def update_output(n_clicks, textarea):

    if n_clicks > 0:
        my_children = html.H5(children=['You have entered: \n{}'.format(textarea)])

    return [my_children]

if __name__ == '__main__':
                   # host="",

Thx in advance!

Hi @liuzc123

If you change the callback to something like this it should work:

    Output(component_id="output_children", component_property="children"),
    Input(component_id="button", component_property="n_clicks"),
    State(component_id="textarea", component_property="value"),
def update_output(n_clicks, textarea):
    if n_clicks > 0:
        return html.H1(
                html.Span("You have entered: ", style={"color": "blue"}),
                html.Span(textarea, style={"color": "red"}),

1 Like

Hello, @AnnMarieW

Right now, I think I should be able to mark it as a solution. :+1: :cowboy_hat_face: Thanks again!

1 Like

Hello @AnnMarieW ,

If we have a long description and we want to color-code phrases into different colors in the description based on those known phrases starting index number and ending index number, how can we do that in Dash?

I have five phrases that I want to color into different colors.

Output would be like this.

Thank you in advance, go Dash!

Hello @AnnMarieW,

Just want to reach out again. Instead of using “html.Span()” to manually color code specific keywords in the whole string and concatenate them together, can we automatically color code string based on known strings starting index number and ending index number?

For example, the description “Game of Thrones is an HBO series that tells the story of a medieval country’s civil war.”

“Game of Thrones” starting index number is 0, and the ending index number is 15.
“HBO” starting index number is 22, and the ending index number is 25.

Ideally output,

Does Dash/ Plotly have this functionality? Thank you in advance and go Dash!

Hi @liuzc123

There is no built-in function that I know of that will color words based on index, but you could create one. :wink:

Okay! Anyway, thanks though. :cowboy_hat_face: