How to force a 'one per line' implementation in the 'text area' module?

Hi guys,

Is there a way to have a “one per line” implementation in the “text area” module?

Similarly to eg what you have on this website: https://httpstatus.io/ - that is, pasting a list of URLs that would then be analysed in the app.

I tried various ways but no success so far. I’ll keep you posted here if I find the solution! :slight_smile:

Thanks,
Charly

Hello @Charly_Wargnier,

You mean getting each line of a text area?

text = st.text_area("URLs, one per line.")
lines = text.split("\n")  # A list of lines
1 Like

@Synode saving the day, again! :raised_hands:

Any way I could limit the number of URLs/Lines? (eg only 100 URLs)

Thanks again! :slight_smile:

1 Like

Haha, even though streamlit offers great possibilities, one of its strength is to be a simple framework upon which you can perform data processing using basic python features :stuck_out_tongue:

You can only limit the number of characters. Along with this parameter, what you could do for lines is to count them with len(lines), and if it is greater than your limit, you can warn the user with a st.warning() that the list will be truncated. For the truncation part, a simple lines = lines[:100] would do.

MAX_LINES = 100

text = st.text_area("URLs, one per line.")
lines = text.split("\n")  # A list of lines

if len(lines) > MAX_LINES:
    st.warning(f"Maximum number of lines reached. Only the first {MAX_LINES} will be processed.")
    lines = lines[:MAX_LINES]

for line in lines:
    # Process line here
1 Like

Ace - Thanks very much Synode!

Although I must confess, conditional statements in lists… I should have thought about that! :stuck_out_tongue:

2 Likes