Skip to content
Snippets Groups Projects
Commit 92f9d6fd authored by Leah Tacke genannt Unterberg's avatar Leah Tacke genannt Unterberg
Browse files

attempting to work with temp files where necessary and work in memory otherwise

parent 071f83b6
Branches
No related tags found
No related merge requests found
import os
import tempfile
import mdata.core.machine_data_def as mdd
import mdata.file_formats.csv.shared
import streamlit as st
from mdata.file_formats.csv import write_machine_data
from mdata.file_formats.csv.exporting import write_machine_data_custom
from mdata.file_formats.hdf import write_machine_data_h5
st.title('Machine Data File Export & Conversions')
......@@ -16,41 +17,29 @@ else:
st.header('File Format Exports')
st.subheader('CSV')
c1, c2 = st.columns(2)
with c1:
c11, c12 = st.columns(2)
with c11:
do_csv_export = st.button('Export as CSV')
with c12:
ht = st.selectbox('Header file type', ['json', 'csv'])
with c2:
c21, c22 = st.columns(2)
header_download = c21.empty()
csv_download = c22.empty()
st.subheader('HDF')
header_format = st.selectbox('Header file type', mdata.file_formats.csv.shared.HeaderFileFormats)
do_csv_export = st.button('Convert to CSV')
header_download = st.empty()
csv_download = st.empty()
c1, c2 = st.columns(2)
with c1:
do_hdf_export = st.button('Export as HDF')
with c2:
st.subheader('HDF')
do_hdf_export = st.button('Convert to HDF')
hdf_download = st.empty()
if do_csv_export:
write_machine_data('temp/temp', md, header_type=ht)
st.session_state['did_csv_export'] = True
with tempfile.NamedTemporaryFile('w+', newline='') as hf:
with tempfile.NamedTemporaryFile('w+', newline='') as df:
write_machine_data_custom(hf, df, md, header_format=header_format)
hf.seek(0)
df.seek(0)
header_download.download_button('download header', hf.file, f'converted_header.{header_format}',
mime=f'text/{header_format}')
csv_download.download_button('download csv file', df.file, 'converted_data.csv', mime=f'text/csv')
elif do_hdf_export:
write_machine_data_h5('temp/temp.h5', md)
hf = f'temp/temp_header.{ht}'
df = 'temp/temp_data.csv'
if os.path.exists(hf) and os.path.exists(df):
with open(hf, 'r') as header_file:
header_download.download_button('download header', header_file, f'converted_header.{ht}')
with open(df, 'r') as csv_file:
csv_download.download_button('download csv file', csv_file, 'converted_data.csv')
h5f = 'temp/temp.h5'
if os.path.exists(h5f):
with open(h5f, 'rb') as hdf_file:
hdf_download.download_button('download h5 file', hdf_file, 'converted.h5')
with tempfile.NamedTemporaryFile('w+b') as hdf:
write_machine_data_h5(hdf.file, md)
hdf.seek(0)
hdf_download.download_button('download h5 file', hdf.file, 'converted.h5')
import os
import tempfile
import streamlit as st
from mdata.file_formats.csv import read_machine_data
from mdata.file_formats.hdf import read_machine_data_h5
......@@ -24,22 +24,27 @@ elif import_type == 'hdf':
# @st.experimental_memo
def import_hdf(f):
fn = os.path.join('uploads', f.name)
with open(fn, 'wb') as fd:
fd.write(f.getbuffer())
fd.flush()
return read_machine_data_h5(fn)
with tempfile.NamedTemporaryFile() as fp:
fp.write(f.getbuffer())
fp.seek(0)
md = read_machine_data_h5(fp.file)
return md
# fn = os.path.join('uploads', f.name)
# with open(fn, 'wb') as fd:
# fd.write(f.getbuffer())
# fd.flush()
# return read_machine_data_h5(fn)
# @st.experimental_memo
def import_csv(hf, df):
header_type = None
header_format = None
if 'csv' in hf.type:
header_type = 'csv'
header_format = 'csv'
elif 'json' in hf.type:
header_type = 'json'
assert header_type is not None
return read_machine_data(hf, df, validity_checking=False, header_type=header_type)
header_format = 'json'
assert header_format is not None
return read_machine_data(hf.getvalue(), df.getvalue(), validity_checking=True, header_format=header_format)
@st.cache_data
......
This diff is collapsed.
......@@ -11,12 +11,12 @@ license = "MIT"
[tool.poetry.dependencies]
python = "^3.9,!=3.9.7"
cvxopt = "^1.3"
streamlit = "^1.21"
streamlit = "^1.23"
# mdata = "^0.1.0"
mdata = { path = "C:/Users/Leah/PycharmProjects/mdata", develop = true }
# mdata = { git = "https://git-ce.rwth-aachen.de/leah.tgu/mdata.git", branch = "master" }
seaborn = "^0.12.2"
plotly = "^5.14.1"
plotly = "^5.15"
tsdownsample = "^0.1.2"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment