Hey guys in today’s video we will be building a very simple Calculator using Streamlit. This is going to be a concise and crisp blog.
So without any further due, let’s do it…
Sneap Peek at Output
Prerequisites
Before we dive into building our calculator, make sure you have Python installed on your system. You’ll also need to install Streamlit using pip:
pip install streamlit
Code for Calculator using Streamlit
## Calculator using Streamlit import streamlit as st # Title and input st.title("Calculator") input_exp = st.text_input("Enter a valid calculation:") # Function to generate nudge def calculate(input_text): return eval(input_text) if st.button("Calculate"): res = calculate(input_exp) st.write(f"Result: {res}")
- You can see the code above for our calculator using Streamlit.
- In the code, we are simply importing streamlit in the very first line.
- Then we have simply set the Title of our app web page.
- In the next line, we are taking the input from the user using the streamlit.text_input() function.
- Then we have a function calculate that will make the calculations from the string that the user has input. We are using the eval() function to accomplish this.
- Then finally we created a button called Calculate using the streamlit.button() function.
- As soon as someone clicks on this button, it will pass the input string to the calculate function, generate results, and show them on the UI.
Running the Calculator
To run your calculator, open a terminal in the folder where your “calculator.py” file is located. Then, run the following command:
streamlit run calculator.py
Streamlit will open a web application in your default web browser, and you’ll be able to use your calculator.
Output
Conclusion
In this article, we’ve walked through the process of building a simple calculator using Streamlit. Streamlit’s simplicity and intuitive interface make it an excellent choice for creating interactive applications with minimal code. You can extend this example by adding more features, customizing the UI, or even integrating it into more complex projects. Happy coding!
FAQ
What is Streamlit?
Streamlit is an open-source Python library used for creating web applications with minimal effort. It’s designed for data scientists and developers to quickly turn data scripts into shareable web apps.
Do I need prior web development experience to use Streamlit?
No, Streamlit is beginner-friendly and doesn’t require extensive web development knowledge. If you’re familiar with Python, you can start creating apps using Streamlit.
What kind of applications can I build with Streamlit?
Streamlit is versatile and can be used to build various web applications, including data dashboards, calculators, data visualizations, and more.
Can I style my Streamlit applications?
Yes, you can style your Streamlit apps by customizing the layout, adding CSS, and using Streamlit’s built-in theming options.
How can I share my Streamlit app with others?
You can easily share your Streamlit app by deploying it to a web hosting service, such as Heroku, or by using Streamlit sharing. This allows others to access your app via a web URL.
Can I add more complex features to my calculator app?
Yes, you can enhance your calculator app by adding features like history logs, unit conversion, or support for more mathematical functions.
Is Streamlit suitable for building production-level applications?
While Streamlit is great for prototyping and building simple applications, it may not be the best choice for highly complex and scalable production applications. For such cases, you might consider using web frameworks like Flask or Django.
Are there any limitations to using Streamlit?
Streamlit is designed for simplicity and quick development, so it might have limitations in terms of advanced customization and complex web app requirements. It’s important to choose the right tool for the specific project.
Can I use Streamlit with Python 2.x?
No, Streamlit is compatible with Python 3.x. It’s recommended to use the latest Python 3 version for the best compatibility and support.
Where can I find more Streamlit resources and documentation?
You can find official Streamlit documentation at streamlit.io, and there is an active Streamlit community where you can ask questions and get support on the Streamlit forums.
Read my last article – Easiest way to train YOLONAS on the custom dataset
Check out my other machine learning projects, deep learning projects, computer vision projects, NLP projects, Flask projects at machinelearningprojects.net