Skip to content

Commit

Permalink
Adding initial files
Browse files Browse the repository at this point in the history
  • Loading branch information
craZAX42 authored Jan 18, 2022
1 parent 9088031 commit 2dbe1ec
Show file tree
Hide file tree
Showing 2 changed files with 238 additions and 0 deletions.
117 changes: 117 additions & 0 deletions SessionState.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
"""Hack to add per-session state to Streamlit.
Usage
-----
>>> import SessionState
>>>
>>> session_state = SessionState.get(user_name='', favorite_color='black')
>>> session_state.user_name
''
>>> session_state.user_name = 'Mary'
>>> session_state.favorite_color
'black'
Since you set user_name above, next time your script runs this will be the
result:
>>> session_state = get(user_name='', favorite_color='black')
>>> session_state.user_name
'Mary'
"""
try:
import streamlit.ReportThread as ReportThread
from streamlit.server.Server import Server
except Exception:
# Streamlit >= 0.65.0
import streamlit.report_thread as ReportThread
from streamlit.server.server import Server


class SessionState(object):
def __init__(self, **kwargs):
"""A new SessionState object.
Parameters
----------
**kwargs : any
Default values for the session state.
Example
-------
>>> session_state = SessionState(user_name='', favorite_color='black')
>>> session_state.user_name = 'Mary'
''
>>> session_state.favorite_color
'black'
"""
for key, val in kwargs.items():
setattr(self, key, val)


def get(**kwargs):
"""Gets a SessionState object for the current session.
Creates a new object if necessary.
Parameters
----------
**kwargs : any
Default values you want to add to the session state, if we're creating a
new one.
Example
-------
>>> session_state = get(user_name='', favorite_color='black')
>>> session_state.user_name
''
>>> session_state.user_name = 'Mary'
>>> session_state.favorite_color
'black'
Since you set user_name above, next time your script runs this will be the
result:
>>> session_state = get(user_name='', favorite_color='black')
>>> session_state.user_name
'Mary'
"""
# Hack to get the session object from Streamlit.

ctx = ReportThread.get_report_ctx()

this_session = None

current_server = Server.get_current()
if hasattr(current_server, '_session_infos'):
# Streamlit < 0.56
session_infos = Server.get_current()._session_infos.values()
else:
session_infos = Server.get_current()._session_info_by_id.values()

for session_info in session_infos:
s = session_info.session
if (
# Streamlit < 0.54.0
(hasattr(s, '_main_dg') and s._main_dg == ctx.main_dg)
or
# Streamlit >= 0.54.0
(not hasattr(s, '_main_dg') and s.enqueue == ctx.enqueue)
or
# Streamlit >= 0.65.2
(not hasattr(s, '_main_dg') and s._uploaded_file_mgr == ctx.uploaded_file_mgr)
):
this_session = s

if this_session is None:
raise RuntimeError(
"Oh noes. Couldn't get your Streamlit Session object. "
'Are you doing something fancy with threads?')

# Got the session object! Now let's attach some state into it.

if not hasattr(this_session, '_custom_session_state'):
this_session._custom_session_state = SessionState(**kwargs)

return this_session._custom_session_state
121 changes: 121 additions & 0 deletions smiles.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import streamlit as st
import streamlit.components.v1 as components
import py3Dmol
import SessionState
from rdkit import Chem
from rdkit.Chem import Draw
from rdkit.Chem import AllChem
import hydralit_components as hc
import hydralit as hy

# @st.cache(allow_output_mutation=True)
# def smile_models():
# return {}

# @st.cache(allow_output_mutation=True, hash_funcs={set: lambda _: None})
# def smile_strings():
# return []

app = hy.HydraApp(title='Simple Multi-Page App')

# st.set_page_config(layout='wide',initial_sidebar_state='collapsed',)

menu_data = [
{'icon': "far fa-copy", 'label':"Left End"},
{'id':'Copy','icon':"🐙",'label':"Copy"},
{'icon': "fa-solid fa-radar",'label':"Dropdown1", 'submenu':[{'id':' subid11','icon': "fa fa-paperclip", 'label':"Sub-item 1"},{'id':'subid12','icon': "💀", 'label':"Sub-item 2"},{'id':'subid13','icon': "fa fa-database", 'label':"Sub-item 3"}]},
]

over_theme = {'txc_inactive': '#FFFFFF'}
menu_id = hc.nav_bar(
menu_definition=menu_data,
override_theme=over_theme,
home_name='Home',
login_name='Logout',
hide_streamlit_markers=False, #will show the st hamburger as well as the navbar now!
sticky_nav=True, #at the top or not
sticky_mode='pinned', #jumpy or not-jumpy, but sticky or pinned
)

@app.addapp()
def my_home():

st.title('SMILES + RDKit + Py3DMOL :smiley:')

ss = SessionState.get(smile_models={}, smile_strings=[])

def get_pymol_style(style):
return style

pymol_style = st.selectbox('Select 3D Style', ('stick','line','sphere'))

def create_models(smi, style=get_pymol_style(pymol_style)):
mol = Chem.MolFromSmiles(smi)
mol = Chem.AddHs(mol)
AllChem.EmbedMolecule(mol)
AllChem.MMFFOptimizeMolecule(mol, maxIters=200)
mblock = Chem.MolToMolBlock(mol)
view = py3Dmol.view(width=450, height=450)
view.addModel(mblock, 'mol')
view.setStyle({style:{}})
view.zoomTo()
view.show()
view.render()
t = view.js()
f = open('viz.html', 'w')
f.write(t.startjs)
f.write(t.endjs)
f.close()

def add_smiles(input_smiles):
"""Take the smile the user input and add it to the list of smile strings
along with their 2d and 3d structure.
Args:
input_smiles (str): The string input of a smile provided by the user
"""
m = Chem.MolFromSmiles(input_smiles)
create_models(input_smiles)
HtmlFile = open("viz.html", 'r', encoding='utf-8')
source_code = HtmlFile.read()

if input_smiles not in ss.smile_strings:
ss.smile_strings.append(input_smiles)
ss.smile_models[input_smiles] = (m, source_code)

def display_smiles(first_smile, second_smile):

c1,c2=st.columns(2)
with c1:
Draw.MolToFile(ss.smile_models[first_smile][0], 'mol.png')
st.image('mol.png')
with c2:
components.html(ss.smile_models[first_smile][1],height=450,width=450)

c1,c2=st.columns(2)
with c1:
Draw.MolToFile(ss.smile_models[second_smile][0], 'mol2.png')
st.image('mol2.png')
with c2:
components.html(ss.smile_models[second_smile][1], height=450,width=450)

input_smiles=st.text_input('Enter SMILES string\nLeft click, hold, then move mouse to rotate 3D view. \
Right click, hold then move mouse to zoom in/out.',\
'O=C1C2=C(N=CN2C)N(C)C(N1C)=O')
print(input_smiles) # For debugging
try:
add_smiles(input_smiles)
except BaseException:
st.write('Invalid SMILES, please input a valid SMILES string.')

################ Sidebar ####################
first_smile = st.sidebar.selectbox('Select your first desired SMILE', ss.smile_strings)
second_smile = st.sidebar.selectbox('Select your second desired SMILE', ss.smile_strings)
if len(ss.smile_strings) >= 2:
display_smiles(first_smile, second_smile)

@app.addapp()
def app2():
hy.info('Hello from app 2')

app.run()

0 comments on commit 2dbe1ec

Please sign in to comment.