Spaces:
Sleeping
Sleeping
#!/usr/bin/env python | |
# coding: utf-8 | |
import pandas as pd | |
import base64 | |
import uuid | |
import io | |
import pickle | |
import string | |
import simplejson as json | |
import regex as re | |
import streamlit as st | |
chars = string.digits + string.ascii_uppercase + string.ascii_lowercase + '_$' | |
tabs = None | |
def compress(g): | |
bs = [int(g[i:i + 2], 16) for i in range(0, len(g), 2)] | |
def b64(v, l=4): | |
return ''.join([chars[(v // (64 ** i)) % 64] for i in range(l)][::-1]) | |
return ''.join([b64(bs[0], 2)] + [b64((bs[i] << 16) + (bs[i + 1] << 8) + bs[i + 2]) for i in range(1, 16, 3)]) | |
def revit_id_to_guid(sheet = pd.DataFrame, column = string): | |
if not '-' in sheet[column]: | |
return sheet[column] | |
id_components = sheet[column].rsplit('-', 1) | |
episode_id = id_components[0] | |
element_id = id_components[1] | |
revit_end_start = episode_id.rsplit('-', 1) | |
episode_id_base10 = int(element_id, 16) | |
revit_end_base10 = int(revit_end_start[1], 16) | |
base10_components = episode_id_base10 ^ revit_end_base10 | |
guid = revit_end_start[0] + '-' + hex(base10_components)[2:] | |
try: | |
guid = compress(uuid.UUID(guid).hex) | |
except ValueError: | |
guid = sheet[column] | |
return guid | |
def download_button(object_to_download, download_filename, button_text, pickle_it=False): | |
""" | |
Generates a link to download the given object_to_download. | |
Params: | |
------ | |
object_to_download: The object to be downloaded. | |
download_filename (str): filename and extension of file. e.g. mydata.csv, | |
some_txt_output.txt download_link_text (str): Text to display for download | |
link. | |
button_text (str): Text to display on download button (e.g. 'click here to download file') | |
pickle_it (bool): If True, pickle file. | |
Returns: | |
------- | |
(str): the anchor tag to download object_to_download | |
Examples: | |
-------- | |
download_link(your_df, 'YOUR_DF.csv', 'Click to download data!') | |
download_link(your_str, 'YOUR_STRING.txt', 'Click to download text!') | |
""" | |
if pickle_it: | |
try: | |
object_to_download = pickle.dumps(object_to_download) | |
except pickle.PicklingError as e: | |
st.write(e) | |
return None | |
else: | |
if isinstance(object_to_download, bytes): | |
pass | |
elif isinstance(object_to_download, pd.DataFrame): | |
#object_to_download = object_to_download.to_csv(index=False) | |
towrite = io.BytesIO() | |
object_to_download = object_to_download.to_excel( | |
towrite, | |
encoding='utf-8', | |
index=False, | |
header=True, | |
na_rep='n/a', | |
) | |
towrite.seek(0) | |
# Try JSON encode for everything else | |
else: | |
object_to_download = json.dumps(object_to_download) | |
try: | |
# some strings <-> bytes conversions necessary here | |
b64 = base64.b64encode(object_to_download.encode()).decode() | |
except AttributeError as e: | |
b64 = base64.b64encode(towrite.read()).decode() | |
button_uuid = str(uuid.uuid4()).replace('-', '') | |
button_id = re.sub('\d+', '', button_uuid) | |
custom_css = f""" | |
<style> | |
#{button_id} {{ | |
display: inline-flex; | |
align-items: center; | |
justify-content: center; | |
background-color: rgb(255, 255, 255); | |
color: rgb(38, 39, 48); | |
padding: .25rem .75rem; | |
position: relative; | |
text-decoration: none; | |
border-radius: 4px; | |
border-width: 1px; | |
border-style: solid; | |
border-color: rgb(230, 234, 241); | |
border-image: initial; | |
}} | |
#{button_id}:hover {{ | |
border-color: rgb(246, 51, 102); | |
color: rgb(246, 51, 102); | |
}} | |
#{button_id}:active {{ | |
box-shadow: none; | |
background-color: rgb(246, 51, 102); | |
color: white; | |
}} | |
</style> """ | |
dl_link = custom_css + f'<a download="{download_filename}" id="{button_id}" href="data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;base64,{b64}">{button_text}</a><br></br>' | |
return dl_link | |
st.markdown( | |
''' | |
# Convert Revit IDs to GUIDs | |
Provide any spreadsheet (using a dropbox link) that has a column of revit IDs. | |
A typical Revit ID is `60f91daf-3dd7-4283-a86d-24137b73f3da-0001fd0b`. | |
This app will convert the ID to `3CPy6r22DAahSW8AIIobik`. | |
Select the sheet witin the spreadsheet and the column with the IDs. | |
Only the selected sheet will be returned, you can cut and paste the sheet back into the origial spreadsheet. | |
''' | |
) | |
cobie_file_button = st.text_input( | |
"Dropbox link to spreadsheet file", | |
key="cobie_file_button" | |
) | |
# In[ ]: | |
if cobie_file_button: | |
cobie_file_path = st.session_state.cobie_file_button | |
if '=0' in cobie_file_path: | |
cobie_file_path = cobie_file_path.replace('=0', '=1') | |
cobie_file = pd.ExcelFile(cobie_file_path) | |
tabs = cobie_file.sheet_names | |
df = pd.DataFrame() | |
if tabs: | |
selected_sheet = None | |
selected_sheet = st.selectbox( | |
'Select sheet for changing revit ids to COBie ids', | |
tabs, | |
) | |
if selected_sheet: | |
df = cobie_file.parse(selected_sheet) | |
if type(df) == pd.DataFrame: | |
# st.write(df) | |
selected_column = None | |
columns = df.columns | |
selected_column = st.selectbox( | |
'Select column with revit ids', | |
columns, | |
) | |
convert = st.button(f"Sheet = '{selected_sheet}' Column = '{selected_column}' Convert IDs to COBie IDs") | |
if convert: | |
# df['COBieGUID'] = df.apply(compress) | |
df[selected_column] = df.apply( | |
revit_id_to_guid, | |
column=selected_column, | |
axis=1 | |
) | |
st.markdown( | |
download_button( | |
df, | |
'IFC_GUIDs.xlsx', | |
'Download sheet with IFC GUIDs', | |
pickle_it=False, | |
), | |
unsafe_allow_html=True, | |
) | |