Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Loading Custom Files #4504

Merged
merged 3 commits into from
Mar 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 23 additions & 20 deletions openbb_terminal/helper_funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -1458,28 +1458,31 @@ def export_data(
# since xlsx does not support datetimes with timezones we need to remove it
df = remove_timezone_from_dataframe(df)

if sheet_name is None:
df.to_excel(saved_path, index=True, header=True)
if sheet_name is None: # noqa: SIM223
df.to_excel(
saved_path,
index=True,
header=True,
)

elif saved_path.exists():
with pd.ExcelWriter(
saved_path,
mode="a",
if_sheet_exists="new",
engine="openpyxl",
) as writer:
df.to_excel(
writer, sheet_name=sheet_name, index=True, header=True
)
else:
if saved_path.exists():
with pd.ExcelWriter(
saved_path,
mode="a",
if_sheet_exists="new",
engine="openpyxl",
) as writer:
df.to_excel(
writer, sheet_name=sheet_name, index=True, header=True
)
else:
with pd.ExcelWriter(
saved_path,
engine="openpyxl",
) as writer:
df.to_excel(
writer, sheet_name=sheet_name, index=True, header=True
)
with pd.ExcelWriter(
saved_path,
engine="openpyxl",
) as writer:
df.to_excel(
writer, sheet_name=sheet_name, index=True, header=True
)
elif saved_path.suffix in [".jpg", ".pdf", ".png", ".svg"]:
figure.show(export_image=saved_path, margin=margin)
else:
Expand Down
2 changes: 1 addition & 1 deletion openbb_terminal/parent_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1110,7 +1110,7 @@ def call_load(self, other_args: List[str]):
"--ticker",
action="store",
dest="ticker",
required="-h" not in other_args,
required="-h" not in other_args and "--help" not in other_args,
help="Stock ticker",
)
parser.add_argument(
Expand Down
9 changes: 8 additions & 1 deletion openbb_terminal/stocks/stocks_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -763,7 +763,14 @@ def load_custom(file_path: str) -> pd.DataFrame:
console.print("[red]File path does not exist.[/red]\n")
return pd.DataFrame()

df = pd.read_csv(file_path)
if file_path.endswith(".csv"):
df = pd.read_csv(file_path)
elif file_path.endswith(".xlsx"):
df = pd.read_excel(file_path)
else:
console.print("[red]File type not supported.[/red]\n")
return pd.DataFrame()

console.print(f"Loaded data has columns: {', '.join(df.columns.to_list())}\n")

# Nasdaq specific
Expand Down