Unit Converter Web Application with Streamlit

Youtube Video:https://youtu.be/xn8_2FRlVLU

Introduction

This is a web application built using Streamlit that allows users to convert units across various categories such as distance, temperature, weight, and pressure. The application provides an easy-to-use interface for selecting units and inputting values, displaying the conversion results dynamically.

Project Prerequisites:

Before running the Unit Converter project, ensure you have the following prerequisites set up on your system:

  1. Python: Make sure Python is installed on your system. You can download and install Python from the official website.
  2. Streamlit: Streamlit is a Python library used for building interactive web applications. You can install it via pip:
pip install streamlit

Getting Started:

Follow these steps to set up and run the Unit Converter project:

Step 1: Install Python and Streamlit

  1. If you haven’t already, download and install Python from the official website.
  2. Open your command line interface (CLI) or terminal.
  3. Install Streamlit by running the following command:
pip install streamlit

Step 2: Create a New Python Script

  1. Open your preferred text editor or Integrated Development Environment (IDE).
  2. Create a new Python script file, e.g., unit_converter.py.

Step 3: Write the Code

Copy and paste the following code into your Python script file:

import streamlit as st

# Distance Converter Function
def distance_converter(from_unit, to_unit, value):
units = {
"Meters": 1,
"Kilometers": 1000,
"Feet": 0.3048,
"Miles": 1609.34,
}
result = value * units[from_unit] / units[to_unit]
return result

# Temperature Converter Function
def temperature_converter(from_unit, to_unit, value):
if from_unit == "Celsius" and to_unit == "Fahrenheit":
result = (value * 9/5) + 32
elif from_unit == "Fahrenheit" and to_unit == "Celsius":
result = (value - 32) * 5/9
else:
result = value
return result

# Weight Converter Function
def weight_converter(from_unit, to_unit, value):
units = {
"Kilograms": 1,
"Grams": 0.001,
"Pounds": 0.453592,
"Ounces": 0.0283495,
}
result = value * units[from_unit] / units[to_unit]
return result

# Pressure Converter Function
def pressure_converter(from_unit, to_unit, value):
units = {
"Pascals": 1,
"Hectopascals": 100,
"Kilopascals": 1000,
"Bar": 100000,
}
result = value * units[from_unit] / units[to_unit]
return result

# Streamlit UI
st.title("Unit Converter")

category = st.selectbox("Select Category", ["Distance", "Temperature", "Weight", "Pressure"])

if category == "Distance":
from_unit = st.selectbox("From", ["Meters", "Kilometers", "Feet", "Miles"])
to_unit = st.selectbox("To", ["Meters", "Kilometers", "Feet", "Miles"])
value = st.number_input("Enter Value")
result = distance_converter(from_unit, to_unit, value)
st.write(f"<p style='font-size:24px;'>{value} {from_unit} is equal to {result:.2f} {to_unit}</p>", unsafe_allow_html=True)

elif category == "Temperature":
from_unit = st.selectbox("From", ["Celsius", "Fahrenheit"])
to_unit = st.selectbox("To", ["Celsius", "Fahrenheit"])
value = st.number_input("Enter Value")
result = temperature_converter(from_unit, to_unit, value)
st.write(f"<p style='font-size:24px;'>{value} {from_unit} is equal to {result:.2f} {to_unit}</p>", unsafe_allow_html=True)

elif category == "Weight":
from_unit = st.selectbox("From", ["Kilograms", "Grams", "Pounds", "Ounces"])
to_unit = st.selectbox("To", ["Kilograms", "Grams", "Pounds", "Ounces"])
value = st.number_input("Enter Value")
result = weight_converter(from_unit, to_unit, value)
st.write(f"<p style='font-size:24px;'>{value} {from_unit} is equal to {result:.2f} {to_unit}</p>", unsafe_allow_html=True)

elif category == "Pressure":
from_unit = st.selectbox("From", ["Pascals", "Hectopascals", "Kilopascals", "Bar"])
to_unit = st.selectbox("To", ["Pascals", "Hectopascals", "Kilopascals", "Bar"])
value = st.number_input("Enter Value")
result = pressure_converter(from_unit, to_unit, value)
st.write(f"<p style='font-size:24px;'>{value} {from_unit} is equal to {result:.2f} {to_unit}</p>", unsafe_allow_html=True)

Step 4: Run the Application

  1. Save the Python script file.
  2. Open your CLI or terminal and navigate to the directory where your Python script is saved.
  3. Run the following command to start the Streamlit application:
streamlit run unit_converter.py

Step 5: Use the Unit Converter

  1. Once the Streamlit application is running, you can choose a category from the dropdown menu.
  2. Select the units and input the value to convert.
  3. The conversion result will be displayed dynamically.

That’s it! You’ve successfully set up and used the Unit Converter project. Feel free to customize the code or add additional features to enhance the application further.

Advertisement