Skip to content

Commit

Permalink
Merge pull request #6 from simonsobs/refactor
Browse files Browse the repository at this point in the history
minor code clean-up
  • Loading branch information
guanyilun authored Nov 28, 2023
2 parents 35d84d6 + c1e96a7 commit 0a279bb
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 30 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
run:
streamlit run src/Home.py --server.address=localhost --browser.gatherUsageStats=false
71 changes: 41 additions & 30 deletions src/pages/1_SATP1.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import json
import datetime as dt
from functools import partial
import yaml

import streamlit as st
from streamlit_timeline import st_timeline
from streamlit_ace import st_ace

from schedlib import policies, core, utils
from scheduler_server.configs import get_config
Expand All @@ -14,47 +15,55 @@
# utility functions
# ====================

def block2dict(block, group=None):
res = {
'id': hash(block),
'content': block.name,
'start': block.t0.isoformat(),
'end': block.t1.isoformat(),
}
if group is not None: res['group'] = group
return res


def seq2visdata(seqs):
# make group
is_list = lambda x: isinstance(x, list)
groups = []
tu.tree_map_with_path(
lambda path, x: groups.append({
'id': utils.path2key(path),
'content': utils.path2key(path)
}),
seqs,
is_leaf=is_list
def block2group(path, block):
key = utils.path2key(path)
if key == '': key = 'root'
return {
'id': key,
'content': key
}
groups = tu.tree_leaves(
tu.tree_map_with_path(
block2group,
seqs,
is_leaf=lambda x: isinstance(x, list)
),
is_leaf=lambda x: 'id' in x,
)

# make items
seqs = tu.tree_leaves(
def block2item(block, group=""):
res = {
'id': hash(block),
'content': block.name,
'start': block.t0.isoformat(),
'end': block.t1.isoformat(),
}
if group != "": res['group'] = group
return res
items = tu.tree_leaves(
tu.tree_map_with_path(
lambda path, x: core.seq_map(
partial(block2dict, group=utils.path2key(path)),
partial(block2item, group=utils.path2key(path)),
core.seq_sort(x, flatten=True)
),
seqs,
is_leaf=is_list
is_leaf=lambda x: isinstance(x, list)
),
is_leaf=lambda x: 'id' in x)
return seqs, groups
is_leaf=lambda x: 'id' in x
)
return items, groups

# ====================
# initialize session state
# ====================

if 'user_config' not in st.session_state:
st.session_state.user_config = {}
if 'user_config_str' not in st.session_state:
st.session_state.user_config_str = ""

if 'commands' not in st.session_state:
st.session_state.commands = ""
Expand All @@ -80,13 +89,15 @@ def seq2visdata(seqs):
end_time = st.time_input("End time (UTC)", value=end_time)

with st.expander("Advanced"):
user_config = st.text_area("Config overwrite:", value=json.dumps(st.session_state.user_config, indent=2), height=300)
# user_config = st.text_area("Config overwrite:", value=json.dumps(st.session_state.user_config, indent=2), height=300)
user_config_str = st_ace(value=st.session_state.user_config_str, language='yaml')
try:
user_config = json.loads(user_config)
user_config = yaml.safe_load(user_config_str)
# save a good config on parsing success
st.session_state.user_config_str = user_config_str
except Exception as e:
st.error('Unable to parse config', icon="🚨")
user_config = {}
st.session_state.user_config = user_config
user_config = yaml.safe_load(st.session_state.user_config_str)

def on_load_schedule():
t0 = dt.datetime.combine(start_date, start_time).astimezone(dt.timezone.utc)
Expand Down

0 comments on commit 0a279bb

Please sign in to comment.