How to add a text input widget in streamlit

In this recipe, we will learn how to add a text input widget in Streamlit. We will also take a look at a Streamlit application consisting of a text input widget.

Recipe Objective: How to add a text input widget in streamlit?

Streamlit allows you to add interactivity directly into the app with the help of widgets. You can add a single-line text input widget to your Streamlit app using "st.text_input". It returns a string.

 Syntax: st.text_input(label, value, max_chars, key, type, help, autocomplete, on_change, args, kwargs)
 Parameters:
   label -> A simple label that explains what this input widget is for
   value -> It is the value that is displayed on this widget on its first render. internally, this will be cast to str.
   max_chars -> The maximum number of characters that can be entered.
   key -> An optional string or int to be used as a unique key for this widget. If omitted, a key will be generated for the widget based on its content. Multiple widgets of same types cannot share the same key.
   type -> The text input's format. This can be "default" (for a standard text input) or "password" (for a text input that conceals the user's inputted value). "default" is the default value.
   help -> An optional tooltip that get displayed next to the select slider.
   autocomplete -> An optional value that will be passed to the autocomplete property of the input element. If this value is left blank, "new-password" will be used for "password" inputs and the empty string for "default" inputs.
   on_change -> An optional callback invoked when there is a change in this select slider's value
   args -> An optional tuple of args that can be passed to the callback
   kwargs -> An optional dictionary of kwargs that can be passed to the call back


Code:

#importing required libraries
import streamlit as st

#adding a single-line text input widget
name = st.text_input('Enter your name: ', 'John Doe')

#displaying the entered text
st.write('Your name is ', name)

To run the app, either create an appname.py file with the above code using any text editor, or if you are using a jupyter notebook, you need to download your .ipynb notebook as a Python (.py) file and run the same using the "streamlit run appname.py" command. Once you run the command, the app will automatically open in your default browser.

What Users are saying..

profile image

Savvy Sahai

Data Science Intern, Capgemini
linkedin profile url

As a student looking to break into the field of data engineering and data science, one can get really confused as to which path to take. Very few ways to do it are Google, YouTube, etc. I was one of... Read More

Relevant Projects

LLM Project to Build and Fine Tune a Large Language Model
In this LLM project for beginners, you will learn to build a knowledge-grounded chatbot using LLM's and learn how to fine tune it.

Build an End-to-End AWS SageMaker Classification Model
MLOps on AWS SageMaker -Learn to Build an End-to-End Classification Model on SageMaker to predict a patient’s cause of death.

Text Classification with Transformers-RoBERTa and XLNet Model
In this machine learning project, you will learn how to load, fine tune and evaluate various transformer models for text classification tasks.

Build Piecewise and Spline Regression Models in Python
In this Regression Project, you will learn how to build a piecewise and spline regression model from scratch in Python to predict the points scored by a sports team.

Machine Learning project for Retail Price Optimization
In this machine learning pricing project, we implement a retail price optimization algorithm using regression trees. This is one of the first steps to building a dynamic pricing model.

BERT Text Classification using DistilBERT and ALBERT Models
This Project Explains how to perform Text Classification using ALBERT and DistilBERT

Abstractive Text Summarization using Transformers-BART Model
Deep Learning Project to implement an Abstractive Text Summarizer using Google's Transformers-BART Model to generate news article headlines.

Build a Text Generator Model using Amazon SageMaker
In this Deep Learning Project, you will train a Text Generator Model on Amazon Reviews Dataset using LSTM Algorithm in PyTorch and deploy it on Amazon SageMaker.

Build Portfolio Optimization Machine Learning Models in R
Machine Learning Project for Financial Risk Modelling and Portfolio Optimization with R- Build a machine learning model in R to develop a strategy for building a portfolio for maximized returns.

Build Classification Algorithms for Digital Transformation[Banking]
Implement a machine learning approach using various classification techniques in Python to examine the digitalisation process of bank customers.