-
Notifications
You must be signed in to change notification settings - Fork 1
/
Home.py
76 lines (43 loc) · 1.94 KB
/
Home.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import pandas as pd
import requests
from datetime import timedelta, date
from bs4 import BeautifulSoup
import streamlit as st
#TODO:Create a function to scrape the current currency rate
def rate_parser(input_curr, output_curr):
url = f"https://www.xe.com/currencyconverter/convert/?Amount=1&From={input_curr}&To={output_curr}"
content = requests.get(url).text
soup = BeautifulSoup(content, 'html.parser')
result_element = soup.find("p", class_="result__BigRate-sc-1bsijpp-1 dPdXSB")
if result_element:
currency_text = result_element.get_text().replace(',', '') # Remove comma
rate = float(currency_text.split()[0])
return rate
else:
print(f"Element not found for {input_curr} to {output_curr}.")
return None
#TODO:Create a function to convert an amount from currency to currency in real time
def convert(base,dest,amount):
rate = rate_parser(base,dest)
new_amount = rate * amount
return new_amount
#TODO:Create a list of all supported currencies
currencies = ["USD","EUR","CAD","MAD","GBP","AUD","JPY"]
#this is just a test lit
#TODO:Expend the list as needed
#TODO:Create the UI using streamlit
st.write("# Python based currency converter")
st.sidebar.write("### Currency converter:")
base = st.sidebar.selectbox("Enter a base currency:",currencies)
dest = st.sidebar.selectbox("Enter a destination currency:",currencies)
amount = st.sidebar.number_input("Enter an amount")
input = st.sidebar.button("Convert")
if input:
current_rate = rate_parser(base,dest)
output = convert(base,dest,amount)
st.success("Success")
st.write(f"## Current exchange rate between {base} and {dest} ")
st.write(f"#### 1 {base} = ")
st.write(f" ## :red[{current_rate}] {dest}")
st.write("## Converted amount:")
st.write(f" ### {amount} {base} = :red[{output}] {dest}")