Creating a Sales Analysis Application with Streamlit: A Practical Approach to Business Intelligence
Tags: streamlit, python, pandas, plotyexpress
Published: 2024-04-19 22:11:56

**Introduction:**
Streamlit is a powerful open-source tool that allows you to create interactive web applications for data analysis and visualization with just a little Python code. In this article, we'll explore how to use Streamlit to build a sales analysis application, providing a practical approach to business intelligence. We'll learn how to generate sample data, visualize key information, create interactive charts, and deploy our application to the cloud.

Setting up the Environment:
To get started, we'll need to install the following Python libraries:



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/osjbzv0mi4xznp2k9wpb.png)

-Streamlit is the main library that will allow us to 
 create the web application.
-Pandas will be used for data manipulation and 
 analysis.
-Plotly will help us create attractive and 
 interactive data visualizations.

Let's explore the code step by step:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r66daj8wcemcmks4xq3n.png)


In this section, we import the necessary libraries and generate a sample dataset using a Pandas DataFrame. This DataFrame contains information about different products, such as their name, category, sales, costs, and region.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kq8mp5rbtvxr59iuww5s.png)


Here, we configure the Streamlit application page by setting a title and a "wide" layout to better utilize the available space. Then, we display the sales data in our application using st.header and st.write.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xaa53ohcths4btq8cah5.png)

In this section, we create an interactive bar chart that displays total sales by region. First, we group the data by region and sum the sales using Pandas. Then, we use the plotly.express library to create the chart and display it in our application with st.plotly_chart.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fg6i4ba1auxfoikwmmys.png)

Similarly, we create an interactive pie chart that shows the distribution of sales by product category.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4q5qake1czn4yrt912y5.png)

Finally, we display some key performance indicators (KPIs) important for a business, such as total sales, total cost, and net profit. We use st.columns to divide the screen into three columns and display a KPI in each one.

**Running the Application:**
**To run the application locally, follow these steps:**

1.-Save the above code in a file called ejemplo.py.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/20wquu6y9igayzv0efz6.png)


2.-Open a terminal or command prompt and navigate to the directory 
 where app.py is located.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/10vhxbvb8piqgk78qwhk.png)


3.-Run the following command: streamlit run app.py.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n6egeivkrdz3qk6r063f.png)


4.-The application will open in your local web browser, and you'll 
  be able to interact with the charts and explore the data from 
  different perspectives.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q1sdlh2kirgzenbumgep.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s0gtgx2pdgq7j8kek3e7.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5lng3h8aa41lonbrzoax.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/awxtyb096zve5tlcjd98.png)





**Deploying to Streamlit Cloud:**
Streamlit Cloud is a service that allows us to deploy our Streamlit applications to the cloud effortlessly. To do so, follow these steps:

1.-Create a repository on GitHub and upload the app.py file.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jv5rvokpounycnzg4uix.png)


2.-Go to https://streamlit.io, log in, and create a new app from your GitHub repository.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wrryu6smjewsuwna9sbj.png)


3.-Streamlit Cloud will deploy your application and provide you with a public URL to access it from anywhere.

https://app-b8tbb4n9vpnghxjbnktkpm.streamlit.app/


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5u6ba1b978x9saia89gp.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ngt7qo62o34n0nr9wkck.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6c38a1vb6mssrv2qdmah.png)



**Next Steps:**
This application is just a basic example, but you can improve and expand it in several ways, such as:

-Loading real data from different sources (databases,CSV files, 
 APIs, etc.).
-Adding more analyses and visualizations, such as line charts,scatter plots, heatmaps, etc.
-Implementing data filtering and sorting functions.
-Generating automatic reports in different formats (PDF, Excel, etc.).
-Integrating user authentication and access control.

The flexibility and potential of Streamlit are 
enormous, allowing you to create more complex and 
customized business intelligence applications based 
on your specific needs.

**Conclusion:**
In this article, we've explored how Streamlit can simplify the creation of interactive web applications for data analysis and business intelligence. We've built an application that analyzes sales data, displays key information through interactive charts and KPIs, and learned how to deploy it to the cloud using Streamlit Cloud.

Streamlit is a powerful tool that combines ease of use with the ability to create complex applications. I encourage you to explore Streamlit and discover how it can help you transform your data into valuable information for decision-making in your business.

I hope this article has been helpful and inspires you to begin your own journey with Streamlit. Enjoy creating interactive and attractive business intelligence applications!