import os import streamlit as st import pandas as pd import math import matplotlib.pyplot as plt # Function to read different file types def read_file(file): try: file_extension = file.name.split(".")[-1] if file_extension == "csv": data = pd.read_csv(file) elif file_extension == "xlsx": data = pd.read_excel(file, engine="openpyxl") else: st.error("Unsupported file format. Please upload a CSV or Excel file.") return None # Guardar el archivo en el directorio actual with open(file.name, "wb") as f: f.write(file.getbuffer()) return data except Exception as e: st.error(f"Error reading file: {e}") return None # Function to display the uploaded data def display_data(data): st.write("### Data Preview") st.dataframe(data.head()) # Function to perform mathematical calculations and store results as separate columns def perform_calculations(data): try: st.write("### Calculations") for column in data.columns: st.write("Calculations for column:", column) column_sum = data[column].sum() column_mean = data[column].mean() column_median = data[column].median() data[f"{column}_sum"] = column_sum data[f"{column}_mean"] = column_mean data[f"{column}_median"] = column_median st.write("Sum:", column_sum) st.write("Mean:", column_mean) st.write("Median:", column_median) st.write("### Updated Data") st.dataframe(data) except Exception as e: st.error(f"Error performing calculations: {e}") # Function to perform mathematical calculations def perform_math(df, selected_columns, operation): try: result = None if operation == "sqrt": result = df[selected_columns].applymap(math.sqrt) elif operation == "log": result = df[selected_columns].applymap(math.log) elif operation == "exp": result = df[selected_columns].applymap(math.exp) elif operation == "sin": result = df[selected_columns].applymap(math.sin) elif operation == "cos": result = df[selected_columns].applymap(math.cos) elif operation == "tan": result = df[selected_columns].applymap(math.tan) elif operation == "multiply": result = df[selected_columns].prod(axis=1) elif operation == "add": result = df[selected_columns].sum(axis=1) elif operation == "subtract": result = df[selected_columns[0]] - df[selected_columns[1]] if result is not None: df[f"{operation}_result"] = result return df except Exception as e: st.error(f"Error performing math operation: {e}") return df def plot_graph(data, graph_type, x_variables, y_variables): try: plt.figure(figsize=(8, 6)) for x_var in x_variables: for y_var in y_variables: if graph_type == "Scatter": plt.scatter(data[x_var], data[y_var], label=f"{x_var} vs {y_var}") elif graph_type == "Line": plt.plot(data[x_var], data[y_var], label=f"{x_var} vs {y_var}") elif graph_type == "Bar": x = range(len(data)) plt.bar(x, data[y_var], label=y_var) plt.xlabel("X Values") plt.ylabel("Y Values") plt.title(f"{graph_type} Plot") plt.legend() st.pyplot() except Exception as e: st.error(f"Error plotting graph: {e}") def main(): try: st.title("Excel-like Data Visualization and Calculations") st.write("Upload a CSV or Excel file and visualize the data") file = st.file_uploader("Upload file", type=["csv", "xlsx"]) if file is not None: data = read_file(file) if data is not None: display_data(data) perform_calculations(data) st.write("### Graph Visualizer") st.write("Select variables for visualization:") graph_type = st.selectbox("Graph Type", options=["Scatter", "Line", "Bar"]) x_variables = st.multiselect("X Variables", options=data.columns) y_variables = st.multiselect("Y Variables", options=data.columns) selected_columns = st.multiselect("Select columns:", options=data.columns) operation = st.selectbox("Select an operation:", ["sqrt", "log", "exp", "sin", "cos", "tan", "multiply", "add", "subtract"]) if st.button("Calculate"): data = perform_math(data, selected_columns, operation) st.write(data) if st.button("Plot"): plot_graph(data, graph_type, x_variables, y_variables) except Exception as e: st.error(f"An error occurred: {e}") if __name__ == "__main__": st.set_page_config(page_title="My Analytics App") main()