logologo
  • AI Tools

    DB Query GeneratorMock InterviewResume BuilderLearning Path GeneratorCheatsheet GeneratorAgentic Prompt GeneratorCompany ResearchCover Letter Generator
  • XpertoAI
  • MVP Ready
  • Resources

    CertificationsTopicsExpertsCollectionsArticlesQuestionsVideosJobs
logologo

Elevate Your Coding with our comprehensive articles and niche collections.

Useful Links

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

Resources

  • Xperto-AI
  • Certifications
  • Python
  • GenAI
  • Machine Learning

Interviews

  • DSA
  • System Design
  • Design Patterns
  • Frontend System Design
  • ReactJS

Procodebase © 2024. 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 Computer Vision with OpenCV

    06/12/2024 | Python

  • LangChain Mastery: From Basics to Advanced

    26/10/2024 | Python

  • Mastering Pandas: From Foundations to Advanced Data Engineering

    25/09/2024 | Python

  • LlamaIndex: Data Framework for LLM Apps

    05/11/2024 | Python

  • Automate Everything with Python: A Complete Guide

    08/12/2024 | Python

Related Articles

  • Mastering Context Window Management in Python with LlamaIndex

    05/11/2024 | Python

  • Mastering Time Series Analysis with Scikit-learn in Python

    15/11/2024 | Python

  • Mastering Missing Data in Pandas

    25/09/2024 | Python

  • Exploring Image Processing with Matplotlib

    05/10/2024 | Python

  • Understanding Python OOP Concepts with Practical Examples

    29/01/2025 | Python

  • Understanding Core Concepts of Scikit-learn

    15/11/2024 | Python

  • Mastering Pandas Data Filtering and Boolean Indexing

    25/09/2024 | Python

Popular Category

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