Skip to content

Commit a3253e4

Browse files
committed
fix: make bokeh and psutil optional dependencies and improve theme handling
1 parent 8df64db commit a3253e4

File tree

3 files changed

+166
-137
lines changed

3 files changed

+166
-137
lines changed

environment.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ dependencies:
1010
- numpy==1.26.4 # pandas and numpy are dependencies of pyopenms, however, pyopenms needs numpy<=1.26.4
1111
- pandas==2.2.1 # Adding pandas with specific version for better compatibility
1212
- pytest==8.0.0 # Adding pytest for running tests
13-
- bokeh==3.3.4 # Required for visualization components
1413

1514
- pip:
1615
# dependencies only available through pip
@@ -20,5 +19,3 @@ dependencies:
2019
- pyopenms==3.2.0
2120
- pyopenms_viz==1.0.0
2221
- streamlit-js-eval
23-
- psutil==7.0.0
24-
- bokeh==3.3.4 # Ensuring bokeh is also installed via pip for CI compatibility

requirements.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ plotly==5.22.0
88
captcha==0.5.0
99
pyopenms_viz==1.0.0
1010
streamlit-js-eval
11-
psutil==7.0.0
12-
bokeh==3.3.4 # Required for visualization components
11+
pandas==2.2.1
12+
pytest==8.0.0

src/common/common.py

Lines changed: 164 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,39 @@
77
from typing import Any
88
from pathlib import Path
99
from streamlit.components.v1 import html
10-
import plotly.io as pio
11-
import matplotlib.pyplot as plt
12-
from bokeh.themes import Theme
13-
from bokeh.io import curdoc
10+
11+
# Optional plotting package imports
12+
try:
13+
import plotly.io as pio
14+
15+
PLOTLY_AVAILABLE = True
16+
except ImportError:
17+
PLOTLY_AVAILABLE = False
18+
19+
try:
20+
import matplotlib.pyplot as plt
21+
22+
MPL_AVAILABLE = True
23+
except ImportError:
24+
MPL_AVAILABLE = False
25+
26+
try:
27+
from bokeh.themes import Theme
28+
from bokeh.io import curdoc
29+
30+
BOKEH_AVAILABLE = True
31+
except ImportError:
32+
BOKEH_AVAILABLE = False
1433

1534
import streamlit as st
1635
import pandas as pd
17-
import psutil
36+
37+
try:
38+
import psutil
39+
40+
PSUTIL_AVAILABLE = True
41+
except ImportError:
42+
PSUTIL_AVAILABLE = False
1843

1944
try:
2045
from tkinter import Tk, filedialog
@@ -31,6 +56,10 @@
3156

3257
@st.fragment(run_every=5)
3358
def monitor_hardware():
59+
if not PSUTIL_AVAILABLE:
60+
st.warning("psutil package not installed. Resource monitoring is disabled.")
61+
return
62+
3463
cpu_progress = psutil.cpu_percent(interval=None) / 100
3564
ram_progress = 1 - psutil.virtual_memory().available / psutil.virtual_memory().total
3665

@@ -543,136 +572,139 @@ def configure_plot_theme():
543572
else:
544573
theme_mode = st.session_state.plot_theme
545574

546-
if theme_mode == "light":
547-
# Light theme settings
548-
plt.style.use("default") # Reset to default style
549-
plt.rcParams.update(
550-
{
551-
# Figure
552-
"figure.facecolor": "white",
553-
"figure.edgecolor": "white",
554-
# Axes
555-
"axes.facecolor": "white",
556-
"axes.edgecolor": "black",
557-
"axes.labelcolor": "black",
558-
"axes.prop_cycle": plt.cycler(
559-
"color",
560-
[
561-
"#1f77b4",
562-
"#ff7f0e",
563-
"#2ca02c",
564-
"#d62728",
565-
"#9467bd",
566-
"#8c564b",
567-
"#e377c2",
568-
"#7f7f7f",
569-
"#bcbd22",
570-
"#17becf",
571-
],
572-
),
573-
# Grid
574-
"grid.color": "lightgray",
575-
"grid.linestyle": "--",
576-
"grid.alpha": 0.5,
577-
# Ticks
578-
"xtick.color": "black",
579-
"ytick.color": "black",
580-
# Text
581-
"text.color": "black",
582-
}
575+
if MPL_AVAILABLE:
576+
if theme_mode == "light":
577+
plt.style.use("default") # Reset to default style
578+
plt.rcParams.update(
579+
{
580+
# Figure
581+
"figure.facecolor": "white",
582+
"figure.edgecolor": "white",
583+
# Axes
584+
"axes.facecolor": "white",
585+
"axes.edgecolor": "black",
586+
"axes.labelcolor": "black",
587+
"axes.prop_cycle": plt.cycler(
588+
"color",
589+
[
590+
"#1f77b4",
591+
"#ff7f0e",
592+
"#2ca02c",
593+
"#d62728",
594+
"#9467bd",
595+
"#8c564b",
596+
"#e377c2",
597+
"#7f7f7f",
598+
"#bcbd22",
599+
"#17becf",
600+
],
601+
),
602+
# Grid
603+
"grid.color": "lightgray",
604+
"grid.linestyle": "--",
605+
"grid.alpha": 0.5,
606+
# Ticks
607+
"xtick.color": "black",
608+
"ytick.color": "black",
609+
# Text
610+
"text.color": "black",
611+
}
612+
)
613+
else:
614+
plt.style.use("dark_background")
615+
plt.rcParams.update(
616+
{
617+
# Figure
618+
"figure.facecolor": "#0E1117",
619+
"figure.edgecolor": "#0E1117",
620+
# Axes
621+
"axes.facecolor": "#0E1117",
622+
"axes.edgecolor": "#FFFFFF", # Brighter white for better contrast
623+
"axes.labelcolor": "#FFFFFF", # Brighter white for better contrast
624+
"axes.prop_cycle": plt.cycler(
625+
"color",
626+
[
627+
"#00B5F7", # Brighter blue
628+
"#FF9E44", # Brighter orange
629+
"#4DFA6F", # Brighter green
630+
"#FF4B4B", # Brighter red
631+
"#C78FFF", # Brighter purple
632+
"#FF8F8F", # Brighter brown
633+
"#FF70D2", # Brighter pink
634+
"#E0E0E0", # Brighter gray
635+
"#EBEF53", # Brighter yellow
636+
"#24E7E7", # Brighter cyan
637+
],
638+
),
639+
# Grid
640+
"grid.color": "#555555", # Slightly brighter grid for better visibility
641+
"grid.linestyle": "--",
642+
"grid.alpha": 0.6, # Increased alpha for better visibility
643+
# Ticks
644+
"xtick.color": "#FFFFFF", # Brighter white for better contrast
645+
"ytick.color": "#FFFFFF", # Brighter white for better contrast
646+
# Text
647+
"text.color": "#FFFFFF", # Brighter white for better contrast
648+
}
649+
)
650+
651+
if PLOTLY_AVAILABLE:
652+
# Configure plotly with theme
653+
pio.templates.default = (
654+
"plotly_white" if theme_mode == "light" else "plotly_dark"
583655
)
584-
# Configure plotly with light theme
585-
pio.templates.default = "plotly_white"
586-
# Configure bokeh with light theme
587-
bokeh_theme = {
588-
"attrs": {
589-
"figure": {
590-
"background_fill_color": "#ffffff",
591-
"border_fill_color": "#ffffff",
592-
"outline_line_color": "#000000",
593-
},
594-
"Axis": {
595-
"axis_line_color": "#000000",
596-
"axis_label_text_color": "#000000",
597-
"major_label_text_color": "#000000",
598-
"major_tick_line_color": "#000000",
599-
"minor_tick_line_color": "#000000",
600-
},
601-
"Grid": {
602-
"grid_line_color": "#e0e0e0",
603-
"grid_line_dash": [6, 4],
604-
"grid_line_alpha": 0.3,
605-
},
606-
"Title": {"text_color": "#000000"},
607-
}
608-
}
609-
else:
610-
# Dark theme settings
611-
plt.style.use("dark_background")
612-
plt.rcParams.update(
613-
{
614-
# Figure
615-
"figure.facecolor": "#0E1117",
616-
"figure.edgecolor": "#0E1117",
617-
# Axes
618-
"axes.facecolor": "#0E1117",
619-
"axes.edgecolor": "#FFFFFF", # Brighter white for better contrast
620-
"axes.labelcolor": "#FFFFFF", # Brighter white for better contrast
621-
"axes.prop_cycle": plt.cycler(
622-
"color",
623-
[
624-
"#00B5F7", # Brighter blue
625-
"#FF9E44", # Brighter orange
626-
"#4DFA6F", # Brighter green
627-
"#FF4B4B", # Brighter red
628-
"#C78FFF", # Brighter purple
629-
"#FF8F8F", # Brighter brown
630-
"#FF70D2", # Brighter pink
631-
"#E0E0E0", # Brighter gray
632-
"#EBEF53", # Brighter yellow
633-
"#24E7E7", # Brighter cyan
634-
],
635-
),
636-
# Grid
637-
"grid.color": "#555555", # Slightly brighter grid for better visibility
638-
"grid.linestyle": "--",
639-
"grid.alpha": 0.6, # Increased alpha for better visibility
640-
# Ticks
641-
"xtick.color": "#FFFFFF", # Brighter white for better contrast
642-
"ytick.color": "#FFFFFF", # Brighter white for better contrast
643-
# Text
644-
"text.color": "#FFFFFF", # Brighter white for better contrast
656+
657+
if BOKEH_AVAILABLE:
658+
# Configure bokeh with theme
659+
if theme_mode == "light":
660+
bokeh_theme = {
661+
"attrs": {
662+
"figure": {
663+
"background_fill_color": "#ffffff",
664+
"border_fill_color": "#ffffff",
665+
"outline_line_color": "#000000",
666+
},
667+
"Axis": {
668+
"axis_line_color": "#000000",
669+
"axis_label_text_color": "#000000",
670+
"major_label_text_color": "#000000",
671+
"major_tick_line_color": "#000000",
672+
"minor_tick_line_color": "#000000",
673+
},
674+
"Grid": {
675+
"grid_line_color": "#e0e0e0",
676+
"grid_line_dash": [6, 4],
677+
"grid_line_alpha": 0.3,
678+
},
679+
"Title": {"text_color": "#000000"},
680+
}
645681
}
646-
)
647-
# Configure plotly with dark theme
648-
pio.templates.default = "plotly_dark"
649-
# Configure bokeh with dark theme
650-
bokeh_theme = {
651-
"attrs": {
652-
"figure": {
653-
"background_fill_color": "#0E1117",
654-
"border_fill_color": "#0E1117",
655-
"outline_line_color": "#FFFFFF", # Brighter white for better contrast
656-
},
657-
"Axis": {
658-
"axis_line_color": "#FFFFFF", # Brighter white for better contrast
659-
"axis_label_text_color": "#FFFFFF", # Brighter white for better contrast
660-
"major_label_text_color": "#FFFFFF", # Brighter white for better contrast
661-
"major_tick_line_color": "#FFFFFF", # Brighter white for better contrast
662-
"minor_tick_line_color": "#FFFFFF", # Brighter white for better contrast
663-
},
664-
"Grid": {
665-
"grid_line_color": "#555555", # Slightly brighter grid
666-
"grid_line_dash": [6, 4],
667-
"grid_line_alpha": 0.4, # Increased alpha for better visibility
668-
},
669-
"Title": {
670-
"text_color": "#FFFFFF" # Brighter white for better contrast
671-
},
682+
else:
683+
bokeh_theme = {
684+
"attrs": {
685+
"figure": {
686+
"background_fill_color": "#0E1117",
687+
"border_fill_color": "#0E1117",
688+
"outline_line_color": "#FFFFFF", # Brighter white for better contrast
689+
},
690+
"Axis": {
691+
"axis_line_color": "#FFFFFF", # Brighter white for better contrast
692+
"axis_label_text_color": "#FFFFFF", # Brighter white for better contrast
693+
"major_label_text_color": "#FFFFFF", # Brighter white for better contrast
694+
"major_tick_line_color": "#FFFFFF", # Brighter white for better contrast
695+
"minor_tick_line_color": "#FFFFFF", # Brighter white for better contrast
696+
},
697+
"Grid": {
698+
"grid_line_color": "#555555", # Slightly brighter grid
699+
"grid_line_dash": [6, 4],
700+
"grid_line_alpha": 0.4, # Increased alpha for better visibility
701+
},
702+
"Title": {
703+
"text_color": "#FFFFFF" # Brighter white for better contrast
704+
},
705+
}
672706
}
673-
}
674-
675-
curdoc().theme = Theme(json=bokeh_theme)
707+
curdoc().theme = Theme(json=bokeh_theme)
676708

677709

678710
def show_fig(

0 commit comments

Comments
 (0)