Skip to content

Commit

Permalink
hotfix/ Charts Theme change not persisting (#5036)
Browse files Browse the repository at this point in the history
* revert and fix bug introduced in #4546

* improve theme change in chart, add `theme.apply_style()` to settings_controller/hub config change
  • Loading branch information
tehcoderer authored May 17, 2023
1 parent 9beba4c commit b54c47e
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 93 deletions.
2 changes: 2 additions & 0 deletions frontend-components/plotly/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ function App() {
python_version: data.python_version,
pywry_version: data.pywry_version,
terminal_version: data.terminal_version,
theme: data.theme,
title,
};
};
Expand Down Expand Up @@ -155,6 +156,7 @@ function App() {
cmd={transformedData.cmd}
title={transformedData.title}
globals={transformedData.globals}
theme={transformedData.theme}
info={info}
/>
);
Expand Down
20 changes: 15 additions & 5 deletions frontend-components/plotly/src/components/Chart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export default function Chart({
cmd,
title,
globals,
theme,
info,
}: {
// @ts-ignore
Expand All @@ -33,6 +34,7 @@ export default function Chart({
cmd: string;
title: string;
globals: any;
theme: string;
info?: any;
}) {
const posthog = usePostHog();
Expand Down Expand Up @@ -219,7 +221,7 @@ export default function Chart({
const TRACES = plotData?.data.filter((trace) =>
trace?.name?.startsWith("Volume")
);
let darkmode = !darkMode;
const darkmode = !darkMode;

window.document.body.style.backgroundColor = darkmode ? "#000" : "#fff";

Expand All @@ -240,15 +242,19 @@ export default function Chart({
.getElementsByTagName("svg")[0]
.setAttribute("viewBox", changeIcon.viewBox);

const volumeColors = {
"#e4003a": "#c80000",
"#00ACFF": "#009600",
const volumeColorsDark = {
"#009600": "#00ACFF",
"#c80000": "#e4003a",
};
const volumeColorsLight = {
"#e4003a": "#c80000",
"#00ACFF": "#009600",
};

const volumeColors = darkmode ? volumeColorsDark : volumeColorsLight;

TRACES.forEach((trace) => {
if (trace.type == "bar")
if (trace.type === "bar")
trace.marker.color = trace.marker.color.map((color) => {
return volumeColors[color] || color;
});
Expand Down Expand Up @@ -367,6 +373,10 @@ export default function Chart({
Plotly.relayout(plotDiv, layout_update);
}
});

if (theme !== "dark") {
setChangeTheme(true);
}
}
}, [plotLoaded]);

Expand Down
2 changes: 1 addition & 1 deletion openbb_terminal/core/plots/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ def get_kwargs(self, title: str = "") -> dict:
"download_path": str(get_current_user().preferences.USER_EXPORTS_DIRECTORY),
}

def start(self, debug: bool = False):
def start(self, debug: bool = False): # pylint: disable=W0221
"""Start the backend WindowManager process."""
if self.isatty:
super().start(debug)
Expand Down
166 changes: 83 additions & 83 deletions openbb_terminal/core/plots/plotly.html

Large diffs are not rendered by default.

5 changes: 1 addition & 4 deletions openbb_terminal/core/plots/plotly_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,7 @@ def apply_console_style(self, style: Optional[str] = None) -> None:

def apply_style(self, style: Optional[str] = "") -> None:
"""Apply the style to the libraries."""
if not style:
style = get_current_user().preferences.CHART_STYLE
style = style or self.plt_style

if style != self.plt_style:
self.load_style(style)
Expand Down Expand Up @@ -1055,7 +1054,6 @@ def show(
if kwargs.pop("margin", True):
self._adjust_margins()

theme.apply_style()
self._apply_feature_flags()
self._xaxis_tickformatstops()

Expand Down Expand Up @@ -1381,7 +1379,6 @@ def to_html(self, *args, **kwargs) -> str:
full_html=False,
)
)
theme.apply_style()
self._apply_feature_flags()
self._xaxis_tickformatstops()

Expand Down
4 changes: 4 additions & 0 deletions openbb_terminal/core/session/local_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,10 @@ def set_chart_style_from_hub(configs: dict):
chart_style = terminal_style.get("chart", None)
if chart_style:
set_preference("CHART_STYLE", chart_style)
# pylint: disable=import-outside-toplevel
from openbb_terminal import theme

theme.apply_style(chart_style)


def set_table_style_from_hub(configs: dict):
Expand Down
1 change: 1 addition & 0 deletions openbb_terminal/settings_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ def call_chart(self, other_args: List[str]):
auth_header=get_current_user().profile.get_auth_header(),
)
console.print("")
theme.apply_style(ns_parser.style)
console.print("Chart theme updated.\n")

@log_start_end(log=logger)
Expand Down

0 comments on commit b54c47e

Please sign in to comment.