Skip to content

Commit

Permalink
Add interface tools
Browse files Browse the repository at this point in the history
  • Loading branch information
ejohb committed Oct 16, 2024
1 parent bc633ea commit f04b42c
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 1 deletion.
5 changes: 5 additions & 0 deletions fmtr/tools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,11 @@
except ImportError as exception:
html = MissingExtraMockModule('html', exception)

try:
from fmtr.tools import interface_tools as interface
except ImportError as exception:
interface = MissingExtraMockModule('interface', exception)


__all__ = [
'config',
Expand Down
76 changes: 76 additions & 0 deletions fmtr/tools/interface_tools.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
from time import sleep

import streamlit as st

from fmtr.tools.logging_tools import logger


class Interface:
"""
Base for using streamlit via classes
"""

PATH = __file__
LAYOUT = 'centered'
NAME = 'Base Interface'

def __init__(self, is_root=False):
"""
Set up page layout and call loop method
"""
logger.debug(f'Running interface loop with state: {st.session_state}...')

if is_root:
self.set_title()

self.loop()

def set_title(self):
"""
Set page title and layout when root interface
"""

st.set_page_config(page_title=self.NAME, layout=self.LAYOUT)
st.title(self.NAME)

def loop(self):
"""
Dummy process to simulate a task
"""

if not st.button('Run Test'):
return
msg = 'Running test...'
with st.spinner(msg):
sleep(3)
st.success("Success!")

@classmethod
def is_streamlit(cls):
"""
Infer whether we are running within StreamLit
"""
return bool(st.context.headers)

@classmethod
def launch(cls):
"""
Launch StreamLit, if not already running - otherwise instantiate to run loop method
"""
if cls.is_streamlit():
cls(is_root=True)
else:
from streamlit.web import bootstrap
bootstrap.run(cls.PATH, False, [], {})
2 changes: 1 addition & 1 deletion fmtr/tools/version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.9.4
0.9.5
1 change: 1 addition & 0 deletions requirements.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
'semantic': ['sentence_transformers', 'pandas'],
'metric': ['pandas'],
'html': ['html2text'],
'interface': ['streamlit'],
}

CONSOLE_SCRIPTS = [
Expand Down

0 comments on commit f04b42c

Please sign in to comment.