Skip to content

AmbreenCodeLab/Unit-Converter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 

Repository files navigation

Certainly! Here's a step-by-step guide on how to create the unit converter app with Streamlit. This guide can be used as a README file:

Unit Converter with Streamlit This application allows you to convert between different units such as meters, kilometers, grams, and kilograms using Streamlit. It provides a simple user interface where users can input a value and select the units for conversion.

Requirements Before running the application, make sure you have the following installed:

Python: Version 3.x or later

Streamlit: You can install it via pip

bash Copy pip install streamlit Step-by-Step Guide to Creating the Unit Converter App

  1. Importing Streamlit python Copy import streamlit as st First, you need to import Streamlit to create the web application. Streamlit is a Python library that allows you to build web applications with minimal effort.

  2. Defining the Converter Function python Copy def converter_units(value, unit_from, unit_to): conversions = { "meters_kilometers": 0.001, # 1 meter = 0.001 kilometer "kilometers_meters": 1000, # 1 kilometer = 1000 meters "grams_kilograms": 0.001, # 1 gram = 0.001 kilogram "kilograms_grams": 1000 # 1 kilogram = 1000 grams }

    key = f"{unit_from}_{unit_to}" # Generate a key based on input and output units

    if key in conversions: return value * conversions[key] else: return "Conversion not supported" This function is responsible for converting the values based on the selected units.

The conversions dictionary contains the conversion factors between different units.

It checks the input units (unit_from) and the output units (unit_to), then performs the necessary calculation.

  1. Creating the User Interface python Copy st.title("Unit Converter") The st.title() function is used to add a title to the app that will be displayed on the webpage.

  2. Input Fields for User python Copy value = st.number_input("Enter the Value:", min_value=0.0, format="%.2f") unit_from = st.selectbox("Convert from:", ["meters", "kilometers", "grams", "kilograms"]) unit_to = st.selectbox("Convert to:", ["meters", "kilometers", "grams", "kilograms"]) st.number_input() is used to create a numeric input field where the user can enter the value to convert.

st.selectbox() creates dropdown menus for selecting the units to convert from and to. The options available are meters, kilometers, grams, and kilograms.

  1. Convert Button and Result Display python Copy if st.button("Convert"): if value is not None: result = converter_units(value, unit_from, unit_to) st.write(f"Converted value: {result}") else: st.write("Please enter a valid number.") st.button() creates a button on the page. When the button is pressed, it triggers the conversion process.

If the user has entered a valid value, it calls the converter_units() function to perform the conversion.

The result is displayed using st.write().

If no value is entered, it will prompt the user to enter a valid number.

How to Run the Application Save the script as a Python file (e.g., unit_converter.py).

Open a terminal or command prompt and navigate to the directory where the script is located.

Run the application using Streamlit:

bash Copy streamlit run unit_converter.py This command will launch the application in your default web browser, where you can interact with the unit converter.

Functionality Overview The application provides a simple user interface where users can input a value and select the units to convert from and to.

Supported conversions include:

Meters to Kilometers

Kilometers to Meters

Grams to Kilograms

Kilograms to Grams

If an unsupported conversion is selected, the app will notify the user that the conversion is not supported.

Customization To add more units or conversion factors, simply expand the conversions dictionary inside the converter_units() function.

For example, to add conversions between liters and milliliters:

python Copy "liters_milliliters": 1000, "milliliters_liters": 0.001 Conclusion This simple unit converter app allows users to convert between different units and displays the result instantly. Streamlit makes it easy to create web applications without the need for complex web development skills.

Feel free to enhance the app with more features or expand the unit conversion functionality as needed!

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages