Create app.py
#3
by harshidagli - opened
app.py
CHANGED
|
@@ -1,40 +1,61 @@
|
|
| 1 |
-
# INSTRUCTIONS:
|
| 2 |
-
# 1. Open a "Terminal" by: View --> Terminal OR just the "Terminal" through the hamburger menu
|
| 3 |
-
# 2. run in terminal with: streamlit run app.py
|
| 4 |
-
# 3. click the "Open in Browser" link that pops up OR click on "Ports" and copy the URL
|
| 5 |
-
# 4. Open a Simple Browswer with View --> Command Palette --> Simple Browser: Show
|
| 6 |
-
# 5. use the URL from prior steps as intput into this simple browser
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
import streamlit as st
|
| 10 |
-
import altair as alt
|
| 11 |
import pandas as pd
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import pandas as pd
|
| 2 |
+
import altair as alt
|
| 3 |
+
import ipywidgets as widgets
|
| 4 |
+
from IPython.display import display, clear_output
|
| 5 |
+
|
| 6 |
+
alt.data_transformers.disable_max_rows()
|
| 7 |
+
|
| 8 |
+
url = "contract_awards_in_investment_project_financing_22-11-2024.csv"
|
| 9 |
+
df = pd.read_csv(url)
|
| 10 |
+
|
| 11 |
+
numeric_columns = df.select_dtypes(include=['float64', 'int64']).columns
|
| 12 |
+
|
| 13 |
+
col1_dropdown = widgets.Dropdown(
|
| 14 |
+
options=numeric_columns,
|
| 15 |
+
value=numeric_columns[0],
|
| 16 |
+
description="Column 1:"
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
col2_dropdown = widgets.Dropdown(
|
| 20 |
+
options=numeric_columns,
|
| 21 |
+
value=numeric_columns[1],
|
| 22 |
+
description="Column 2:"
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
output = widgets.Output()
|
| 26 |
+
|
| 27 |
+
def update_correlation(change):
|
| 28 |
+
with output:
|
| 29 |
+
clear_output(wait=True)
|
| 30 |
+
|
| 31 |
+
# Get the selected columns
|
| 32 |
+
col1 = col1_dropdown.value
|
| 33 |
+
col2 = col2_dropdown.value
|
| 34 |
+
|
| 35 |
+
# Calculate correlation
|
| 36 |
+
correlation_value = df[col1].corr(df[col2])
|
| 37 |
+
print(f"Correlation between {col1} and {col2}: {correlation_value:.2f}")
|
| 38 |
+
|
| 39 |
+
# Create a scatter plot
|
| 40 |
+
correlation_plot = (
|
| 41 |
+
alt.Chart(df)
|
| 42 |
+
.mark_circle(size=60)
|
| 43 |
+
.encode(
|
| 44 |
+
x=alt.X(f"{col1}:Q", title=col1),
|
| 45 |
+
y=alt.Y(f"{col2}:Q", title=col2),
|
| 46 |
+
color="Region:N",
|
| 47 |
+
tooltip=["Project Name", "Supplier", col1, col2],
|
| 48 |
+
)
|
| 49 |
+
.properties(
|
| 50 |
+
title=f"Correlation Plot: {col1} vs. {col2}",
|
| 51 |
+
width=600,
|
| 52 |
+
height=400
|
| 53 |
+
)
|
| 54 |
+
)
|
| 55 |
+
display(correlation_plot)
|
| 56 |
+
|
| 57 |
+
col1_dropdown.observe(update_correlation, names='value')
|
| 58 |
+
col2_dropdown.observe(update_correlation, names='value')
|
| 59 |
+
|
| 60 |
+
print("Select columns for correlation analysis:")
|
| 61 |
+
display(col1_dropdown, col2_dropdown, output)
|