Spaces:
Runtime error
Runtime error
| import streamlit as st | |
| import pandas as pd | |
| import numpy as np | |
| import matplotlib.pyplot as plt | |
| from matplotlib import cm | |
| #print('cho chet that') | |
| # Plotly imports | |
| from plotly.subplots import make_subplots | |
| import plotly.graph_objects as go | |
| import plotly.figure_factory as ff | |
| import plotly.express as px | |
| plt.rcParams['font.size']=14 | |
| plt.rcParams['font.family']='arial' | |
| import io | |
| from io import StringIO | |
| #from io import BytesIO | |
| def load_data(uploaded_file): | |
| if uploaded_file is not None: | |
| try: | |
| bytes_data = uploaded_file.read() | |
| str_io = StringIO(bytes_data.decode('Windows-1252')) | |
| except UnicodeDecodeError as e: | |
| st.error(f"error loading png: {e}") | |
| else: | |
| str_io = None | |
| return str_io | |
| def export_kvl(df): | |
| st.title("Convert elevation to the kvl format") | |
| ele=df['Z'].to_numpy() | |
| ele=np.vstack(ele) | |
| nx=st.number_input('Input nx',value=100, placeholder="Type a number...") | |
| ny=st.number_input('Input ny',value=81, placeholder="Type a number...") | |
| surface= ele.reshape(nx, ny) | |
| plot_surface= st.checkbox('Plot surface') | |
| if plot_surface: | |
| x_vals = np.arange(0, nx, 1) | |
| y_vals = np.arange(0, ny, 1) | |
| X, Y = np.meshgrid(y_vals, x_vals) | |
| cmap = plt.cm.plasma | |
| fig, ax = plt.subplots() | |
| ax.set_title('Surface grid visualization') | |
| #ax.imshow(surface, cmap=cmap) | |
| cp=ax.contourf(X, Y, surface, cmap=cm.jet) | |
| plt.colorbar(cp) | |
| fig.set_size_inches(6, 5) | |
| st.pyplot(fig) | |
| header = "TOP = \n" | |
| file_tmp = io.StringIO() | |
| #with open(buf, 'w') as file: | |
| # Write the header | |
| # file.write(header + '\n\n') | |
| # Write the array with formatted float numbers | |
| # np.savetxt(file, surface, fmt='%.2f') | |
| #buf.seek(0) | |
| file_tmp.write(header) | |
| np.savetxt(file_tmp, surface, fmt='%.2f') | |
| file_tmp.seek(0) | |
| file_content = file_tmp.getvalue() | |
| filename=st.text_input('Save surface file',value="surface.kvl", placeholder="Type a file name...") | |
| st.download_button( | |
| label="Save and download top file as kvl", | |
| data=file_content, | |
| file_name=filename, | |
| mime="text/plain" | |
| ) | |