logologo
  • AI Interviewer
  • Features
  • Jobs
  • AI Tools
  • FAQs
logologo

Transform your hiring process with AI-powered interviews. Screen candidates faster and make better hiring decisions.

Useful Links

  • Contact Us
  • Privacy Policy
  • Terms & Conditions
  • Refund & Cancellation
  • About Us

Resources

  • Certifications
  • Topics
  • Collections
  • Articles
  • Services

AI Tools

  • AI Interviewer
  • Xperto AI
  • AI Pre-Screening

Procodebase © 2025. All rights reserved.

Level Up Your Skills with Xperto-AI

A multi-AI agent platform that helps you level up your development skills and ace your interview preparation to secure your dream job.

Launch Xperto-AI

Creating Your First Streamlit App

author
Generated by
ProCodebase AI

15/11/2024

python

Sign in to read full article

Introduction to Streamlit

Streamlit is a game-changer in the world of Python web development. It allows data scientists and developers to quickly turn data scripts into shareable web apps without the need for extensive web development knowledge. In this guide, we'll walk through the process of creating your first Streamlit app, setting you on the path to building more complex and interactive data applications.

Setting Up Your Environment

Before we dive into coding, let's set up our development environment:

  1. Install Streamlit using pip:

    pip install streamlit
    
  2. Create a new Python file, let's call it first_app.py.

Building Your First Streamlit App

Let's create a simple app that allows users to input their name and displays a personalized greeting. Here's the code:

import streamlit as st def main(): st.title("My First Streamlit App") # Get user input user_name = st.text_input("Enter your name") # Display greeting if user_name: st.write(f"Hello, {user_name}! Welcome to Streamlit!") if __name__ == "__main__": main()

Let's break down this code:

  1. We import the Streamlit library as st.
  2. We define a main() function to contain our app's logic.
  3. st.title() sets the main title of our app.
  4. st.text_input() creates a text box for user input.
  5. st.write() displays text on the app.

Running Your App

To run your Streamlit app, open a terminal, navigate to the directory containing your Python file, and run:

streamlit run first_app.py

Your default web browser should open automatically, displaying your app.

Adding More Elements

Streamlit offers a wide range of elements to make your app more interactive. Let's enhance our app with a few more features:

import streamlit as st import random def main(): st.title("My Enhanced Streamlit App") # Get user input user_name = st.text_input("Enter your name") # Display greeting if user_name: st.write(f"Hello, {user_name}! Welcome to Streamlit!") # Add a button if st.button("Get a random number"): st.write(f"Here's your random number: {random.randint(1, 100)}") # Add a slider age = st.slider("How old are you?", 0, 120, 25) st.write(f"You're {age} years old!") # Add a selectbox color = st.selectbox("What's your favorite color?", ["Red", "Green", "Blue"]) st.write(f"Your favorite color is {color}") if __name__ == "__main__": main()

This enhanced version includes:

  • A button that generates a random number
  • A slider for selecting age
  • A dropdown (selectbox) for choosing a favorite color

Styling Your App

Streamlit allows for easy styling of your app. Here's how you can add some basic styling:

import streamlit as st st.set_page_config(page_title="My Awesome App", page_icon=":smiley:") # Custom CSS st.markdown(""" <style> .big-font { font-size:20px !important; } </style> """, unsafe_allow_html=True) st.title("Styled Streamlit App") st.markdown('<p class="big-font">This is a paragraph with a custom style!</p>', unsafe_allow_html=True)

This example demonstrates:

  • Setting a custom page title and icon
  • Adding custom CSS
  • Applying custom styles to specific elements

Deploying Your App

Once you're happy with your app, you might want to share it with others. Streamlit offers a free hosting service called Streamlit Cloud. Here's a quick guide to deploying your app:

  1. Push your code to a GitHub repository.
  2. Sign up for a free account at streamlit.io.
  3. Connect your GitHub account and select the repository containing your Streamlit app.
  4. Choose the Python file you want to deploy.
  5. Click 'Deploy', and your app will be live in minutes!

Conclusion

Congratulations! You've just created your first Streamlit app. This is just the beginning of what you can achieve with Streamlit. As you continue to explore, you'll discover how to integrate data visualization, machine learning models, and more complex interactions into your apps.

Remember, the key to mastering Streamlit is practice and experimentation. Don't be afraid to try out different elements and see how they work together. Happy coding!

Popular Tags

pythonstreamlitweb development

Share now!

Like & Bookmark!

Related Collections

  • Mastering Pandas: From Foundations to Advanced Data Engineering

    25/09/2024 | Python

  • Mastering NumPy: From Basics to Advanced

    25/09/2024 | Python

  • Mastering Scikit-learn from Basics to Advanced

    15/11/2024 | Python

  • LangChain Mastery: From Basics to Advanced

    26/10/2024 | Python

  • Automate Everything with Python: A Complete Guide

    08/12/2024 | Python

Related Articles

  • Debugging and Testing Python Code

    21/09/2024 | Python

  • Mastering NumPy Array Indexing and Slicing

    25/09/2024 | Python

  • Advanced File Handling and Data Serialization in Python

    15/01/2025 | Python

  • Mastering Middleware and CORS Handling in FastAPI

    15/10/2024 | Python

  • Exploring Seaborn's Built-in Datasets

    06/10/2024 | Python

  • Unleashing the Power of NumPy

    25/09/2024 | Python

  • Unleashing the Power of Custom Tools and Function Calling in LangChain

    26/10/2024 | Python

Popular Category

  • Python
  • Generative AI
  • Machine Learning
  • ReactJS
  • System Design