tagirshin commited on
Commit
cfc3c2c
1 Parent(s): b968659

added custom download button

Browse files
Files changed (2) hide show
  1. README.md +1 -1
  2. app.py +127 -30
README.md CHANGED
@@ -1,6 +1,6 @@
1
  ---
2
  title: VQGAE
3
- emoji: 📚
4
  colorFrom: blue
5
  colorTo: gray
6
  sdk: streamlit
 
1
  ---
2
  title: VQGAE
3
+ emoji: 🧪
4
  colorFrom: blue
5
  colorTo: gray
6
  sdk: streamlit
app.py CHANGED
@@ -1,4 +1,9 @@
 
 
 
1
  import pickle
 
 
2
 
3
  import numpy as np
4
  import pandas as pd
@@ -14,6 +19,89 @@ from streamlit.components.v1 import html
14
  MoleculeContainer.depict_settings(aam=False)
15
 
16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  def render_svg(svg_string):
18
  """Renders the given svg string."""
19
  c = st.container()
@@ -330,36 +418,45 @@ if submit:
330
 
331
  filtered_gen_stats = valid_gen_stats.iloc[filtered_indices]
332
 
333
- with st.form("results"):
334
- st.subheader('Generation results', divider='rainbow')
335
- st.dataframe(filtered_gen_stats)
336
- st.download_button(
337
- label="Download results as CSV",
338
- data=convert_df(filtered_gen_stats),
339
- file_name='vqgae_tubulin_inhibitors_valid.csv',
340
- mime='text/csv',
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
341
  )
342
 
343
- st.subheader('Examples of generated molecules')
344
- examples_smiles = filtered_gen_stats.sort_values(by=["similarity_score"], ascending=False).iloc[:6].smiles.to_list()
345
- examples = []
346
- for smi in examples_smiles:
347
- mol = smiles(smi)
348
- mol.clean2d()
349
- examples.append(mol)
350
- svg = grid_depict(examples, 2)
351
- render_svg(svg)
352
-
353
- with st.expander("Show full stats"):
354
- st.dataframe(full_stats)
355
-
356
- st.download_button(
357
- label="Download full results as CSV",
358
- data=convert_df(full_stats),
359
- file_name='vqgae_tubulin_inhibitors_full.csv',
360
- mime='text/csv',
361
- )
362
- restart = st.form_submit_button('Restart')
363
-
364
- if restart:
365
  st.rerun()
 
1
+ import base64
2
+ import os
3
+ import json
4
  import pickle
5
+ import uuid
6
+ import re
7
 
8
  import numpy as np
9
  import pandas as pd
 
19
  MoleculeContainer.depict_settings(aam=False)
20
 
21
 
22
+ def download_button(object_to_download, download_filename, button_text, pickle_it=False):
23
+ """
24
+ Generates a link to download the given object_to_download.
25
+ Params:
26
+ ------
27
+ object_to_download: The object to be downloaded.
28
+ download_filename (str): filename and extension of file. e.g. mydata.csv,
29
+ some_txt_output.txt download_link_text (str): Text to display for download
30
+ link.
31
+ button_text (str): Text to display on download button (e.g. 'click here to download file')
32
+ pickle_it (bool): If True, pickle file.
33
+ Returns:
34
+ -------
35
+ (str): the anchor tag to download object_to_download
36
+ Examples:
37
+ --------
38
+ download_link(your_df, 'YOUR_DF.csv', 'Click to download data!')
39
+ download_link(your_str, 'YOUR_STRING.txt', 'Click to download text!')
40
+ """
41
+ if pickle_it:
42
+ try:
43
+ object_to_download = pickle.dumps(object_to_download)
44
+ except pickle.PicklingError as e:
45
+ st.write(e)
46
+ return None
47
+
48
+ else:
49
+ if isinstance(object_to_download, bytes):
50
+ pass
51
+
52
+ elif isinstance(object_to_download, pd.DataFrame):
53
+ object_to_download = object_to_download.to_csv(index=False)
54
+
55
+ # Try JSON encode for everything else
56
+ else:
57
+ object_to_download = json.dumps(object_to_download)
58
+
59
+ try:
60
+ # some strings <-> bytes conversions necessary here
61
+ b64 = base64.b64encode(object_to_download.encode()).decode()
62
+
63
+ except AttributeError as e:
64
+ b64 = base64.b64encode(object_to_download).decode()
65
+
66
+ button_uuid = str(uuid.uuid4()).replace('-', '')
67
+ button_id = re.sub('\d+', '', button_uuid)
68
+
69
+ custom_css = f"""
70
+ <style>
71
+ #{button_id} {{
72
+ background-color: rgb(255, 255, 255);
73
+ color: rgb(38, 39, 48);
74
+ padding: 0.25em 0.38em;
75
+ position: relative;
76
+ text-decoration: none;
77
+ border-radius: 4px;
78
+ border-width: 1px;
79
+ border-style: solid;
80
+ border-color: rgb(230, 234, 241);
81
+ border-image: initial;
82
+ }}
83
+ #{button_id}:hover {{
84
+ border-color: rgb(246, 51, 102);
85
+ color: rgb(246, 51, 102);
86
+ }}
87
+ #{button_id}:active {{
88
+ box-shadow: none;
89
+ background-color: rgb(246, 51, 102);
90
+ color: white;
91
+ }}
92
+ </style> """
93
+
94
+ dl_link = custom_css + f'<a download="{download_filename}" id="{button_id}" href="data:file/txt;base64,{b64}">{button_text}</a><br></br>'
95
+
96
+ return dl_link
97
+
98
+
99
+ def file_selector(folder_path='.'):
100
+ filenames = os.listdir(folder_path)
101
+ selected_filename = st.selectbox('Select a file', filenames)
102
+ return os.path.join(folder_path, selected_filename)
103
+
104
+
105
  def render_svg(svg_string):
106
  """Renders the given svg string."""
107
  c = st.container()
 
418
 
419
  filtered_gen_stats = valid_gen_stats.iloc[filtered_indices]
420
 
421
+ st.subheader('Generation results', divider='rainbow')
422
+ st.dataframe(filtered_gen_stats)
423
+ download_button(
424
+ object_to_download=convert_df(filtered_gen_stats),
425
+ download_filename='vqgae_tubulin_inhibitors_valid.csv',
426
+ button_text="Download results as CSV"
427
+ )
428
+ # st.download_button(
429
+ # label="Download results as CSV",
430
+ # data=convert_df(filtered_gen_stats),
431
+ # file_name='vqgae_tubulin_inhibitors_valid.csv',
432
+ # mime='text/csv',
433
+ # )
434
+
435
+ st.subheader('Examples of generated molecules')
436
+ examples_smiles = filtered_gen_stats.sort_values(by=["similarity_score"], ascending=False).iloc[:6].smiles.to_list()
437
+ examples = []
438
+ for smi in examples_smiles:
439
+ mol = smiles(smi)
440
+ mol.clean2d()
441
+ examples.append(mol)
442
+ svg = grid_depict(examples, 2)
443
+ render_svg(svg)
444
+
445
+ with st.expander("Show full stats"):
446
+ st.dataframe(full_stats)
447
+
448
+ download_button(
449
+ object_to_download=convert_df(full_stats),
450
+ download_filename='vqgae_tubulin_inhibitors_full.csv',
451
+ button_text="Download full results as CSV"
452
  )
453
 
454
+ # st.download_button(
455
+ # label="Download full results as CSV",
456
+ # data=convert_df(full_stats),
457
+ # file_name='vqgae_tubulin_inhibitors_full.csv',
458
+ # mime='text/csv',
459
+ # )
460
+
461
+ if st.button("Restart"):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
462
  st.rerun()