Skip to content

Commit

Permalink
Merge branch 'develop' into hotfix/remove-envs
Browse files Browse the repository at this point in the history
  • Loading branch information
hjoaquim authored Apr 3, 2023
2 parents fbb1034 + f4c48dc commit ff9275d
Show file tree
Hide file tree
Showing 21 changed files with 248 additions and 111 deletions.
7 changes: 7 additions & 0 deletions frontend-components/tables/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,13 @@ function App() {
title={title}
data={transformedData.data}
columns={transformedData.columns}
initialTheme={
data.theme &&
typeof data.theme === "string" &&
data.theme === "dark"
? "dark"
: "light"
}
/>
)}
</DndProvider>
Expand Down
23 changes: 18 additions & 5 deletions frontend-components/tables/src/components/Table/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,22 @@ function getCellWidth(row, column) {
}
}

export default function Table({ data, columns, title }: any) { // source = ""
const [colorTheme, setTheme] = useDarkMode();
export default function Table({
data,
columns,
title,
initialTheme,
}: {
data: any[];
columns: any[];
title: string;
initialTheme: "light" | "dark";
}) {
const [colorTheme, setTheme] = useDarkMode(initialTheme);
const [darkMode, setDarkMode] = useState(
colorTheme === "light" ? true : false
colorTheme === "dark" ? true : false
);
const toggleDarkMode = (checked) => {
const toggleDarkMode = (checked: boolean) => {
setTheme(colorTheme);
setDarkMode(checked);
};
Expand Down Expand Up @@ -553,7 +563,10 @@ export default function Table({ data, columns, title }: any) { // source = ""
<th
key={header.id}
colSpan={header.colSpan}
className="text-grey-500 bg-grey-100 dark:bg-grey-850 font-normal text-left text-sm h-10"
className="text-grey-500 bg-grey-100 dark:bg-grey-850 font-normal text-left text-sm h-10 p-4"
style={{
width: header.getSize(),
}}
>
{header.isPlaceholder
? null
Expand Down
5 changes: 2 additions & 3 deletions frontend-components/tables/src/utils/useDarkMode.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { useState, useEffect } from "react";

export default function useDarkMode() {
const [theme, setTheme] = useState(localStorage.theme);
export default function useDarkMode(initialTheme: "dark" | "light") {
const [theme, setTheme] = useState(initialTheme);
const colorTheme = theme === "dark" ? "light" : "dark";

useEffect(() => {
const root = window.document.documentElement;
root.classList.remove(colorTheme);
root.classList.add(theme);
localStorage.setItem("theme", theme);
}, [theme, colorTheme]);

return [colorTheme, setTheme];
Expand Down
11 changes: 10 additions & 1 deletion openbb_terminal/common/common_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
@log_start_end(log=logger)
def load(
file: str,
sheet_name: Optional[str] = None,
data_files: Optional[Dict[Any, Any]] = None,
data_examples: Optional[Dict[Any, Any]] = None,
) -> pd.DataFrame:
Expand Down Expand Up @@ -90,7 +91,15 @@ def load(

try:
if file_type == ".xlsx":
data = pd.read_excel(full_file)
try:
data = (
pd.read_excel(full_file)
if sheet_name is None
else pd.read_excel(full_file, sheet_name=sheet_name)
)
except ValueError as err:
console.print(f"[red]{err}[/red]\n")
return pd.DataFrame()
elif file_type == ".csv":
data = pd.read_csv(full_file)
else:
Expand Down
5 changes: 4 additions & 1 deletion openbb_terminal/core/log/generation/settings_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ def log_all_settings(with_rollover: bool = True) -> None:

def log_system() -> None:
"""Log system"""
logger.info("SYSTEM: %s ", json.dumps(get_current_system().to_dict()))
system_dict = get_current_system().to_dict()
system_dict.pop("LOGGING_AWS_ACCESS_KEY_ID", None)
system_dict.pop("LOGGING_AWS_SECRET_ACCESS_KEY", None)
logger.info("SYSTEM: %s ", json.dumps(system_dict))


def log_credentials() -> None:
Expand Down
4 changes: 2 additions & 2 deletions openbb_terminal/core/models/preferences_model.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import os
from typing import Optional
from typing import Literal, Optional

from pydantic import NonNegativeInt, PositiveFloat, PositiveInt
from pydantic.dataclasses import dataclass
Expand Down Expand Up @@ -86,7 +86,7 @@ class PreferencesModel(BaseModel):
MPL_STYLE: str = "dark"
PMF_STYLE: str = "dark"
RICH_STYLE: str = "dark"
PLOT_STYLE: str = "dark"
THEME: Literal["dark", "light"] = "dark"

# PATHS
PREFERRED_DATA_SOURCE_FILE: str = str(USER_DATA_SOURCES_DEFAULT_FILE)
Expand Down
11 changes: 10 additions & 1 deletion openbb_terminal/core/models/system_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,16 @@

@dataclass(config=dict(validate_assignment=True, frozen=True))
class SystemModel(BaseModel):
"""Data model for system variables and configurations."""
"""
Data model for system variables and configurations.
Disclaimer:
If you need to have a system related variable that is a credential like
`LOGGING_AWS_ACCESS_KEY_ID` and `LOGGING_AWS_SECRET_ACCESS_KEY`, you need
refer to the following function
`openbb_terminal.core.log.generation.settings_logger.log_system`,
in order to filter it from the logs.
"""

# System section
OS: str = str(platform.system())
Expand Down
12 changes: 10 additions & 2 deletions openbb_terminal/core/plots/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,13 @@ async def process_image(self, export_image: Path):
opener = "open" if sys.platform == "darwin" else "xdg-open"
subprocess.check_call([opener, export_image]) # nosec: B603

def send_table(self, df_table: pd.DataFrame, title: str = "", source: str = ""):
def send_table(
self,
df_table: pd.DataFrame,
title: str = "",
source: str = "",
theme: str = "dark",
):
"""Send table data to the backend to be displayed in a table.
Parameters
Expand All @@ -233,6 +239,8 @@ def send_table(self, df_table: pd.DataFrame, title: str = "", source: str = ""):
Title to display in the window, by default ""
source : str, optional
Source of the data, by default ""
theme : light or dark, optional
Theme of the table, by default "light"
"""
self.loop.run_until_complete(self.check_backend())

Expand Down Expand Up @@ -261,7 +269,7 @@ def send_table(self, df_table: pd.DataFrame, title: str = "", source: str = ""):
width = max(int(min(sum(columnwidth) * 9.7, self.WIDTH + 100)), 800)

json_data = json.loads(df_table.to_json(orient="split"))
json_data.update(dict(title=title, source=source or ""))
json_data.update(dict(title=title, source=source or "", theme=theme or "dark"))

self.outgoing.append(
json.dumps(
Expand Down
4 changes: 2 additions & 2 deletions openbb_terminal/core/plots/plotly_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def apply_console_style(self, style: Optional[str] = "") -> None:
def apply_style(self, style: Optional[str] = "") -> None:
"""Apply the style to the libraries."""
if not style:
style = get_current_user().preferences.PLOT_STYLE
style = get_current_user().preferences.THEME

if style != self.plt_style:
self.load_style(style)
Expand Down Expand Up @@ -244,7 +244,7 @@ def get_colors(self, reverse: bool = False) -> list:


theme = TerminalStyle(
get_current_user().preferences.PLOT_STYLE, get_current_user().preferences.RICH_STYLE
get_current_user().preferences.THEME, get_current_user().preferences.RICH_STYLE
)
theme.apply_style()

Expand Down
64 changes: 32 additions & 32 deletions openbb_terminal/core/plots/table.html

Large diffs are not rendered by default.

11 changes: 8 additions & 3 deletions openbb_terminal/core/session/constants.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
BASE_URL = "https://payments.openbb.dev/"
REGISTER_URL = "https://my.openbb.dev/register"
SOURCES_URL = "https://my.openbb.dev/app/terminal/datasources"
KEYS_URL = "https://my.openbb.dev/app/terminal/apikeys"
# BASE_URL = "http://127.0.0.1:8000/"

HUB_URL = "https://my.openbb.dev/"
REGISTER_URL = HUB_URL + "register"
SOURCES_URL = HUB_URL + "app/terminal/datasources"
KEYS_URL = HUB_URL + "app/terminal/apikeys"
COLORS_URL = HUB_URL + "app/terminal/theme"
CHARTS_TABLES_URL = HUB_URL + "app/terminal/theme/charts-tables"

TIMEOUT = 15

CONNECTION_ERROR_MSG = "[red]Connection error.[/red]"
Expand Down
6 changes: 3 additions & 3 deletions openbb_terminal/core/session/hub_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ def fetch_user_configs(
def upload_config(
key: str,
value: str,
type_: Literal["keys", "settings", "sources"],
type_: Literal["keys", "settings", "sources", "terminal_style"],
auth_header: str,
base_url: str = BASE_URL,
timeout: int = TIMEOUT,
Expand All @@ -248,7 +248,7 @@ def upload_config(
The key to patch.
value : str
The value to patch.
type_ : Literal["keys", "settings", "sources"]
type_ : Literal["keys", "settings", "sources", "terminal_style"]
The type of the patch.
auth_header : str
The authorization header, e.g. "Bearer <token>".
Expand All @@ -262,7 +262,7 @@ def upload_config(
Optional[requests.Response]
The response from the patch request.
"""
if type_ not in ["keys", "settings", "sources"]:
if type_ not in ["keys", "settings", "sources", "terminal_style"]:
console.print("[red]\nInvalid patch type.[/red]")
return None

Expand Down
37 changes: 28 additions & 9 deletions openbb_terminal/core/session/local_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,11 @@ def apply_configs(configs: dict):
configs : dict
The configurations.
"""
# Saving the RICH_STYLE state allows user to change from hub rich style to local
set_credentials_from_hub(configs)
set_preferences_from_hub(configs, ["RICH_STYLE", "PLOT_STYLE"])
save_theme_from_hub(configs)
set_preferences_from_hub(configs, fields=["RICH_STYLE"])
set_rich_style_from_hub(configs)
set_chart_table_style_from_hub(configs)
set_sources_from_hub(configs)


Expand Down Expand Up @@ -154,8 +156,8 @@ def set_preferences_from_hub(configs: dict, fields: Optional[List[str]] = None):
set_preference(k, v)


def save_theme_from_hub(configs: dict):
"""Set theme.
def set_rich_style_from_hub(configs: dict):
"""Set rich style from hub.
Parameters
----------
Expand All @@ -165,9 +167,9 @@ def save_theme_from_hub(configs: dict):
if configs:
terminal_style = configs.get("features_terminal_style", {}) or {}
if terminal_style:
user_style = terminal_style.get("theme", None)
if user_style:
user_style = {k: v.replace(" ", "") for k, v in user_style.items()}
rich_style = terminal_style.get("theme", None)
if rich_style:
rich_style = {k: v.replace(" ", "") for k, v in rich_style.items()}
try:
with open(
MISCELLANEOUS_DIRECTORY
Expand All @@ -176,14 +178,31 @@ def save_theme_from_hub(configs: dict):
/ "hub.richstyle.json",
"w",
) as f:
json.dump(user_style, f)
json.dump(rich_style, f)

# Default to hub theme
preferences = configs.get("features_settings", {}) or {}
if "RICH_STYLE" not in preferences:
set_preference("RICH_STYLE", "hub")

except Exception:
console.print("[red]Failed to save theme.[/red]")
console.print("[red]Failed to set rich style.[/red]")


def set_chart_table_style_from_hub(configs: dict):
"""Set chart and table style from hub.
Parameters
----------
configs : dict
The configurations.
"""
if configs:
terminal_style = configs.get("features_terminal_style", {}) or {}
if terminal_style:
chart_table = terminal_style.get("chart_table", None)
if chart_table:
set_preference("THEME", chart_table)


def set_sources_from_hub(configs: dict):
Expand Down
17 changes: 16 additions & 1 deletion openbb_terminal/econometrics/econometrics_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ def __init__(self, queue: Optional[List[str]] = None):
"-a": "-alias",
"--examples": None,
"-e": "--examples",
"--sheet-name": None,
}

for feature in ["export", "show", "desc", "clear", "index"]:
Expand Down Expand Up @@ -349,6 +350,13 @@ def call_load(self, other_args: List[str]):
default=False,
dest="examples",
)
parser.add_argument(
"--sheet-name",
dest="sheet_name",
default=None,
nargs="+",
help="Name of excel sheet to save data to. Only valid for .xlsx files.",
)

if other_args and "-" not in other_args[0][0]:
other_args.insert(0, "-f")
Expand Down Expand Up @@ -405,7 +413,14 @@ def call_load(self, other_args: List[str]):
)
return

data = common_model.load(file, self.DATA_FILES, common_model.DATA_EXAMPLES)
data = common_model.load(
file,
data_files=self.DATA_FILES,
data_examples=common_model.DATA_EXAMPLES,
sheet_name=" ".join(ns_parser.sheet_name)
if ns_parser.sheet_name
else None,
)

if not data.empty:
data.columns = data.columns.map(lambda x: x.lower().replace(" ", "_"))
Expand Down
13 changes: 9 additions & 4 deletions openbb_terminal/economy/economy_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -1389,8 +1389,13 @@ def call_fred(self, other_args: List[str]):
query = " ".join(ns_parser.query)
df_search = fred_model.get_series_notes(search_query=query)

if not df_search.empty:
fred_view.notes(search_query=query, limit=ns_parser.limit)
if isinstance(df_search, pd.DataFrame) and not df_search.empty:
fred_view.notes(
search_query=query,
limit=ns_parser.limit,
export=ns_parser.export,
sheet_name=ns_parser.sheet_name,
)

self.fred_query = df_search["id"].head(ns_parser.limit)
self.update_runtime_choices()
Expand Down Expand Up @@ -1431,7 +1436,7 @@ def call_fred(self, other_args: List[str]):
get_data=True,
)

if not df.empty:
if isinstance(df, pd.DataFrame) and not df.empty:
for series_id, data in detail.items():
self.FRED_TITLES[
series_id
Expand All @@ -1448,7 +1453,7 @@ def call_fred(self, other_args: List[str]):
if get_current_user().preferences.ENABLE_EXIT_AUTO_HELP:
self.print_help()

else:
elif not ns_parser.export and not ns_parser.raw:
console.print("[red]No data found for the given Series ID[/red]")

elif not parameters and ns_parser.raw:
Expand Down
Loading

0 comments on commit ff9275d

Please sign in to comment.