Page MenuHomec4science

dscript_gui.py
No OneTemporary

File Metadata

Created
Tue, Sep 17, 06:52

dscript_gui.py

import streamlit as st
from PIL import Image
import dscript
time_unit = 0.001 # 1 ms
# Check if the ip address is valid
def is_valid_ipv4(ip):
try:
return all(0 <= int(num) < 256 for num in ip.split('.')) and len(ip.split('.')) == 4
except ValueError:
return False
img = Image.open('./favicon.ico')
st.set_page_config(page_title="dscript driver", page_icon=img)
st.title("dscript driver")
infobox = st.empty()
def ip_changed():
ip = st.session_state.ip_address
if is_valid_ipv4(ip):
dscript.ip_address = ip
else:
st.error('Invalid IP address')
st.text_input('Module ip address', value=dscript.ip_address, key='ip_address', on_change=ip_changed)
def set_gate(position: int, switch: int, state: bool):
infobox.info(f'{"C" if state else "Disc"}onnecting gate...')
try:
dscript.set_gate(position, switch, state)
infobox.success(f'Gate {i + 1} {"" if state else "dis"}connected')
except Exception as e:
infobox.error(type(e).__name__ + ': ' + str(e))
col1, col2, _, col3, col4 = st.columns([2, 2, 1, 2, 2])
with col1:
st.subheader('Switch 1')
for i in range(6):
if st.button(f'๐ŸŸข Connect {i + 1}', key=f'connect1-{i + 1}', use_container_width=True):
set_gate(i + 1, 1, True)
with col2:
st.subheader('ใ…ค')
for i in range(6):
if st.button(f'๐Ÿ”ด Disconnect {i + 1}', key=f'disconnect1-{i + 1}', use_container_width=True):
set_gate(i + 1, 1, False)
with col3:
st.subheader('Switch 2')
for i in range(6):
if st.button(f'๐ŸŸข Connect {i + 1}', key=f'connect2-{i + 1}', use_container_width=True):
set_gate(i + 1, 2, True)
with col4:
st.subheader('ใ…ค')
for i in range(6):
if st.button(f'๐Ÿ”ด Disconnect {i + 1}', key=f'disconnect2-{i + 1}', use_container_width=True):
set_gate(i + 1, 2, False)
st.text('\n')
if st.button('๐Ÿ” Reset All Relays', key='reset'):
dscript.reset_relays()
st.divider()
def numeric_changed():
st.session_state.slider = st.session_state.numeric
dscript.pulse_duration = st.session_state.numeric * time_unit
def slider_changed():
st.session_state.numeric = st.session_state.slider
dscript.pulse_duration = st.session_state.slider * time_unit
if 'slider' not in st.session_state:
st.session_state.slider = int(dscript.pulse_duration / time_unit)
if 'numeric' not in st.session_state:
st.session_state.numeric = int(dscript.pulse_duration / time_unit)
st.number_input('Pulse edge duration (ms)', min_value=0, step=10,
key='numeric', on_change=numeric_changed, help='โŽ')
st.slider('slider', min_value=0, max_value=200,
key='slider', on_change=slider_changed, label_visibility='hidden')
st.text('\n')
def change_timeout():
dscript.connection_timeout = round(st.session_state.connection_timeout, 1)
def port_changed():
dscript.port = st.session_state.port
def slowdown_changed():
dscript.slowdown = st.session_state.slowdown * time_unit
with st.expander("Advanced"):
st.text('\n')
st.number_input('Slowdown (ms)', min_value=0, value=int(dscript.slowdown / time_unit), step=10,
key='slowdown', on_change=slowdown_changed,
help='Amount of time that the driver will wait for the relay before sending the next command.')
st.number_input('Connection timeout (s)', min_value=0.0, value=dscript.connection_timeout, step=0.1, format='%.1f',
key='connection_timeout', on_change=change_timeout)
st.number_input('Port', min_value=0, max_value=65535, value=dscript.port, step=1, key='port',
on_change=port_changed)
st.text('\n')
st.text('Copyright ยฉ 2023. LPQM')
hide_streamlit_style = """
<style>
#MainMenu {visibility: hidden;}
footer {visibility: hidden;}
</style>
"""
st.markdown(hide_streamlit_style, unsafe_allow_html=True)

Event Timeline