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

hotfix/economy-charts #4387

Merged
merged 10 commits into from
Mar 3, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -746,7 +746,7 @@ def display_data(..., plot: bool = False, ...):
...
if plot:
...
ax.plot(...)
fig.add_scatter(...)
```

<br>
Expand Down
37 changes: 30 additions & 7 deletions openbb_terminal/core/plots/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@
import aiohttp
import pandas as pd
import plotly.graph_objects as go
from pywry import PyWry
import pywry
from packaging import version
from reportlab.graphics import renderPDF
from svglib.svglib import svg2rlg

from openbb_terminal.base_helpers import strtobool
from openbb_terminal.base_helpers import console, strtobool
from openbb_terminal.core.config.paths import USER_EXPORTS_DIRECTORY

try:
from IPython import get_ipython
Expand All @@ -39,7 +41,7 @@
BACKEND = None


class Backend(PyWry):
class Backend(pywry.PyWry):
"""Custom backend for Plotly."""

def __new__(cls, *args, **kwargs): # pylint: disable=W0613
Expand Down Expand Up @@ -147,7 +149,7 @@ def send_figure(
json.dumps(
{
"html_path": self.get_plotly_html(),
"plotly": json.loads(fig.to_json()),
"json_data": json.loads(fig.to_json()),
"export_image": str(export_image).replace(".pdf", ".svg"),
**self.get_kwargs(title),
}
Expand Down Expand Up @@ -195,14 +197,24 @@ def send_table(self, df_table: pd.DataFrame, title: str = ""):
Title to display in the window, by default ""
"""
self.loop.run_until_complete(self.check_backend())
columnwidth = [
max(len(str(df_table[col].name)), df_table[col].astype(str).str.len().max())
for col in df_table.columns
]
# we add a percentage of max to the min column width
columnwidth = [
int(x + (max(columnwidth) - min(columnwidth)) * 0.2) for x in columnwidth
]

self.outgoing.append(
json.dumps(
{
"html_path": self.get_table_html(),
"plotly": df_table.to_json(orient="split"),
"width": self.WIDTH,
"height": self.HEIGHT,
"json_data": df_table.to_json(orient="split"),
"width": int(max(sum(columnwidth) * 9.5, self.WIDTH + 100)),
"height": int(
min(len(df_table.index) * 25 + 25, self.HEIGHT + 100)
),
**self.get_kwargs(title),
}
)
Expand Down Expand Up @@ -267,6 +279,7 @@ def get_kwargs(self, title: str = "") -> dict:
return {
"title": f"OpenBB - {title}",
"icon": self.get_window_icon(),
"download_path": str(USER_EXPORTS_DIRECTORY),
}

def del_temp(self):
Expand All @@ -282,6 +295,16 @@ def start(self, debug: bool = False):
async def check_backend(self):
"""Override to check if isatty."""
if self.isatty:
if not hasattr(pywry, "__version__") or version.parse(
pywry.__version__
) < version.parse("0.3.5"):
console.print(
"[bold red]Pywry version 0.3.5 or higher is required to use the "
"OpenBB Plots backend.[/bold red]\n"
"[yellow]Please update pywry with 'pip install pywry --upgrade'[/yellow]"
)
self.max_retries = 0
return
await super().check_backend()

def close(self, reset: bool = False):
Expand Down
2 changes: 1 addition & 1 deletion openbb_terminal/core/plots/plotly.html
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
// calling OpenBBMain to avoid errors
if (typeof OpenBBMain != "undefined" && window.plotly_figure) {
clearInterval(interval);
OpenBBMain(window.plotly_figure);
OpenBBMain(window.plotly_figure, document.getElementById("openbb_chart"));
}
}, 20);
</script>
Expand Down
3 changes: 3 additions & 0 deletions openbb_terminal/core/plots/plotly_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -926,6 +926,9 @@ def show(
self._xaxis_tickformatstops()

self.update_traces(marker_line_width=self.bar_width, selector=dict(type="bar"))
self.update_traces(
selector=dict(type="scatter"), hovertemplate="y: %{y}<br>x: %{x}"
)

# Set modebar style
if self.layout.template.layout.mapbox.style == "dark": # type: ignore
Expand Down
6 changes: 3 additions & 3 deletions openbb_terminal/core/plots/table.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@
<div id="example-table"></div>
<script type="text/javascript">
var interval = setInterval(function () {
// Waits for OpenBBMain and plotly_figure to be defined before
// Waits for OpenBBMain and json_data to be defined before
// calling OpenBBMain to avoid errors
if (typeof TableProcessor != "undefined" && window.plotly_figure) {
if (typeof TableProcessor != "undefined" && window.json_data) {
clearInterval(interval);
TableProcessor(window.plotly_figure);
TableProcessor(window.json_data);
}
}, 20);
</script>
Expand Down
20 changes: 10 additions & 10 deletions openbb_terminal/core/plots/web/bar_menus.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ function downloadData(gd) {
: "y";

if (candlestick) {
csv = "Date,Open,High,Low,Close\r";
csv = "Date,Open,High,Low,Close\n";
data.forEach(function (trace) {
if (trace.type == "candlestick") {
let x = trace.x;
Expand All @@ -235,7 +235,7 @@ function downloadData(gd) {
let close = trace.close;

for (let i = 0; i < x.length; i++) {
csv += `${x[i]},${open[i]},${high[i]},${low[i]},${close[i]}\r`;
csv += `${x[i]},${open[i]},${high[i]},${low[i]},${close[i]}\n`;
}
}
});
Expand All @@ -248,35 +248,35 @@ function downloadData(gd) {
});

if (traces == 1) {
csv = `${xaxis},${yaxis}\r`;
csv = `${data[0].name},${xaxis},${yaxis}\n`;
data.forEach(function (trace) {
if (trace.type == "scatter") {
let x = trace.x;
let y = trace.y;

for (let i = 0; i < x.length; i++) {
csv += `${x[i]},${y[i]}\r`;
csv += `${x[i]},${y[i]}\n`;
}
}
});
} else if (traces > 1) {
csv = `${xaxis},`;
csv = `${xaxis}`;
data.forEach(function (trace) {
if (trace.type == "scatter") {
csv += `${trace.name},`;
csv += `,${trace.name}`;
}
});
csv += "\r";
csv += "\n";

let x = data[0].x;
for (let i = 0; i < x.length; i++) {
csv += `${x[i]},`;
csv += `${x[i]}`;
data.forEach(function (trace) {
if (trace.type == "scatter") {
csv += `${trace.y[i]},`;
csv += `,${trace.y[i]}`;
}
});
csv += "\r";
csv += "\n";
}
} else {
return;
Expand Down
16 changes: 11 additions & 5 deletions openbb_terminal/core/plots/web/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ let check_divs = setInterval(function () {
}
}, 100);

function OpenBBMain(plotly_figure) {

function OpenBBMain(plotly_figure, chartdiv) {
// Main function that plots the graphs and initializes the bar menus
CHART_DIV = chartdiv;
globals.chartDiv = CHART_DIV;
console.log("main.js loaded");
console.log("plotly_figure", plotly_figure);
Expand Down Expand Up @@ -283,12 +285,14 @@ function OpenBBMain(plotly_figure) {
} else {
is_3dmesh = true;
}

});

// send a relayout event to trigger the initial zoom/bars-resize
// check if the xaxis.range is defined
if (graphs.layout.xaxis != undefined && graphs.layout.xaxis.range != undefined) {
if (
graphs.layout.xaxis != undefined &&
graphs.layout.xaxis.range != undefined
) {
Plotly.relayout(CHART_DIV, {
"xaxis.range[0]": graphs.layout.xaxis.range[0],
"xaxis.range[1]": graphs.layout.xaxis.range[1],
Expand Down Expand Up @@ -323,7 +327,10 @@ function OpenBBMain(plotly_figure) {
let close_interval = is_3dmesh ? 1000 : 500;

// We get the extension of the file and check if it is valid
const extension = window.export_image.split(".").pop().replace("jpg", "jpeg");
const extension = window.export_image
.split(".")
.pop()
.replace("jpg", "jpeg");

if (["jpeg", "png", "svg"].includes(extension)) {
// We run Plotly.downloadImage to save the chart as an image
Expand All @@ -333,7 +340,6 @@ function OpenBBMain(plotly_figure) {
height: CHART_DIV.clientHeight,
filename: window.export_image.split("/").pop(),
});

}
setTimeout(function () {
window.close();
Expand Down
2 changes: 2 additions & 0 deletions openbb_terminal/economy/econdb_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -743,6 +743,8 @@ def get_aggregated_macro_data(
else:
denomination_string = f" [in {denomination}]" if denomination != "Units" else ""

df_rounded = df_rounded.dropna()

return (df_rounded, units, denomination_string)


Expand Down
10 changes: 4 additions & 6 deletions openbb_terminal/economy/econdb_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
# pylint:disable=too-many-arguments,unused-argument
import logging
import os

# from textwrap import fill
from typing import Optional, Union

from openbb_terminal import OpenBBFigure
Expand Down Expand Up @@ -88,31 +86,31 @@ def show_macro_data(
x=df_rounded.index,
y=df_rounded[column],
mode="lines",
name=country_label + " " + parameter_label,
name=f"{country_label} [{parameter_label}, {parameter_units}]",
)
fig.set_title(f"Macro data{denomination}")
elif len(parameters) > 1:
fig.add_scatter(
x=df_rounded.index,
y=df_rounded[column],
mode="lines",
name=country_label + " " + parameter_label,
name=f"{parameter_label} [{parameter_units}]",
)
fig.set_title(f"{country_label}{denomination}")
elif len(countries) > 1:
fig.add_scatter(
x=df_rounded.index,
y=df_rounded[column],
mode="lines",
name=country_label + " " + parameter_label,
name=f"{country_label} [{parameter_units}]",
)
fig.set_title(f"{parameter_label}{denomination}")
else:
fig.add_scatter(
x=df_rounded.index,
y=df_rounded[column],
mode="lines",
name=country_label + " " + parameter_label,
name=f"{country_label} [{parameter_label}, {parameter_units}]",
)
fig.set_title(
f"{parameter_label} of {country_label}{denomination} [{parameter_units}]"
Expand Down
7 changes: 5 additions & 2 deletions openbb_terminal/economy/economy_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,10 @@ def call_macro(self, other_args: List[str]):
if other_args and "-" not in other_args[0][0]:
other_args.insert(0, "-p")
ns_parser = self.parse_known_args_and_warn(
parser, other_args, export_allowed=EXPORT_ONLY_RAW_DATA_ALLOWED, raw=True
parser,
other_args,
export_allowed=EXPORT_BOTH_RAW_DATA_AND_FIGURES,
raw=True,
)
if ns_parser:
parameters = list_from_str(ns_parser.parameters.upper())
Expand Down Expand Up @@ -1289,7 +1292,7 @@ def call_plot(self, other_args: List[str]):
ns_parser = self.parse_known_args_and_warn(
parser,
other_args,
export_allowed=EXPORT_ONLY_RAW_DATA_ALLOWED,
export_allowed=EXPORT_BOTH_RAW_DATA_AND_FIGURES,
limit=10,
)

Expand Down
1 change: 1 addition & 0 deletions openbb_terminal/economy/fred_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,5 +388,6 @@ def get_cpi(

df = pd.DataFrame.from_dict(series_dictionary)
df.index = pd.to_datetime(df.index).date
df = df.dropna()

return df
6 changes: 1 addition & 5 deletions openbb_terminal/economy/fred_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,11 +214,7 @@ def plot_cpi(
"growth_previous": "Growth Previous Period (%)",
}

country = [
country.replace("_", " ").title()
for country in countries
if df.columns.str.contains(country)
]
country = str(countries[0]).replace("_", " ").title()

title = f"{'Harmonized ' if harmonized else ''} Consumer Price"
title += (
Expand Down
Binary file removed openbb_terminal/fixedincome/commercial_paper.xlsx
Binary file not shown.
Binary file not shown.
Binary file removed openbb_terminal/fixedincome/ice_bofa_indices.xlsx
Binary file not shown.
Loading