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/PyWry Linux Wheels-Import Error Handing #4561

Merged
merged 16 commits into from
Mar 23, 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ logs/
.DS_Store
*.env
.venv
venv*/
venv
.vscode
*.ipynb
Expand Down
6 changes: 0 additions & 6 deletions build/docker/openbb.dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ RUN apt-get -y install --no-install-recommends \
libwebkit2gtk-4.0-dev \
build-essential \
libssl-dev \
libgtk-3-dev \
libayatana-appindicator3-dev \
librsvg2-dev \
ffmpeg \
python3-tk
Expand All @@ -44,12 +42,8 @@ WORKDIR /home/python
# SETUP POETRY IMAGE
FROM debian as poetry

RUN curl https://sh.rustup.rs -sSf | sh -s -- -y --default-toolchain nightly

ENV PATH="/home/python/.cargo/bin:${PATH}"
ENV PATH="/home/python/.local/bin:${PATH}"

RUN /bin/bash -c "source $HOME/.cargo/env"
RUN pip install --upgrade pip wheel
RUN pip install poetry==1.3.2

Expand Down
2 changes: 2 additions & 0 deletions openbb_terminal/core/models/preferences_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class PreferencesModel(BaseModel):
PLOT_WIDTH_PERCENTAGE: PositiveFloat = 70.0
# Whether to open plot image exports after they are created
PLOT_OPEN_EXPORT: bool = False
# Use interactive window to display plots
PLOT_ENABLE_PYWRY: bool = True

# FEATURE FLAGS
SYNC_ENABLED: bool = True
Expand Down
9 changes: 8 additions & 1 deletion openbb_terminal/core/plots/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,17 @@
import aiohttp
import pandas as pd
import plotly.graph_objects as go
import pywry
from packaging import version
from reportlab.graphics import renderPDF
from svglib.svglib import svg2rlg

from openbb_terminal.base_helpers import console, strtobool

try:
import pywry
except ImportError:
from openbb_terminal.core.plots import no_import as pywry # pylint: disable=C0412

from openbb_terminal.core.session.current_user import get_current_user

try:
Expand Down Expand Up @@ -62,6 +67,8 @@ def __init__(self, daemon: bool = True, max_retries: int = 30):
and not strtobool(os.environ.get("OPENBB_ENABLE_QUICK_EXIT", False))
and current_process().name == "MainProcess"
)
if pywry.__version__ == "0.0.0":
self.isatty = False

self.WIDTH, self.HEIGHT = 1400, 762

Expand Down
77 changes: 77 additions & 0 deletions openbb_terminal/core/plots/no_import.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import asyncio
from typing import List

import dotenv

from openbb_terminal.base_helpers import console
from openbb_terminal.core.config.paths import SETTINGS_ENV_FILE
from openbb_terminal.core.session.current_user import get_current_user

__version__ = "0.0.0"

pywry_missing = """
[red]PyWry is not installed or missing required linux dependencies.[/]

[yellow]Install PyWry[/]
[green]pip install pywry --upgrade[/]

[yellow]Platform-specific notes[/]
Here is the underlying web engine each platform uses you might need to install.

[green]Linux[/]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice

Pywry uses gtk-rs and its related libraries for window creation and Wry also needs WebKitGTK for WebView.
To activate interactive plots/tables in pywry window, please make sure the following packages are installed:

[yellow]Arch Linux / Manjaro:[/]
[green]sudo pacman -S webkit2gtk-4.0[/]\n
[yellow]Debian / Ubuntu:[/]
[green]sudo apt install libwebkit2gtk-4.0-dev[/]\n
[yellow]Fedora / CentOS / AlmaLinux:[/]
[green]sudo dnf install gtk3-devel webkit2gtk4.0-devel[/]\r
"""


class PyWry:
"""Dummy class to avoid import errors."""

max_retries = 0
outgoing: List[str] = []
init_engine: List[str] = []
daemon = True
debug = False
shell = False
base = None

try:
loop: asyncio.AbstractEventLoop = asyncio.get_event_loop()
except RuntimeError:
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)

def __init__(self, *args, **kwargs): # pylint: disable=W0613
"""Dummy init to avoid import errors."""
current_user = get_current_user()

# If pywry is not installed or missing required linux dependencies
# we inform the user the required packages to install and revert
# plotly default behaviour to open in browser.
# We do this only once.
if current_user.preferences.PLOT_ENABLE_PYWRY:
console.print(pywry_missing)
if console.input(
"If you prefer to continue without interactive plots/tables, "
"press [green]enter[/] or [red]ctrl+c[/] to exit."
):
dotenv.set_key(SETTINGS_ENV_FILE, "PLOT_ENABLE_PYWRY", "0")

current_user.preferences.USE_INTERACTIVE_DF = False

def close(self, *args, **kwargs): # pylint: disable=W0613
"""Dummy method to avoid errors."""

def start(self, debug: bool = False): # pylint: disable=W0613
"""Dummy method to avoid errors."""

async def check_backend(self):
"""Dummy check backend method to avoid errors and revert to browser."""
raise Exception
9 changes: 5 additions & 4 deletions openbb_terminal/core/plots/plotly_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ def __init__(self, fig: Optional[go.Figure] = None, **kwargs) -> None:
self._feature_flags_applied = False
self._exported = False
self._cmd_xshift = 0
self._bar_width = 0.0001
self._bar_width = 0.2
self._subplot_xdates: Dict[int, Dict[int, List[Any]]] = {}

if xaxis := kwargs.pop("xaxis", None):
Expand Down Expand Up @@ -806,7 +806,7 @@ def horizontal_legend(

@staticmethod
def chart_volume_scaling(
df_volume: pd.DataFrame, range_x: int = 4
df_volume: pd.DataFrame, range_x: int = 7
) -> Dict[str, list]:
"""Takes df_volume and returns volume_ticks, tickvals for chart volume scaling

Expand All @@ -815,7 +815,7 @@ def chart_volume_scaling(
df_volume : pd.DataFrame
Dataframe of volume (e.g. df_volume = df["Volume"])
range_x : int, optional
Number to multiply volume, by default 4
Number to multiply volume, by default 7

Returns
-------
Expand Down Expand Up @@ -879,9 +879,10 @@ def add_inchart_volume(
yaxis="y2",
row=row,
col=col,
opacity=0.9,
secondary_y=False,
)
ticksize = 14 - (self.subplots_kwargs["rows"] // 1.5)
ticksize = 14 - (self.subplots_kwargs["rows"] // 2)
self.update_layout(
yaxis=dict(
fixedrange=True,
Expand Down
2 changes: 1 addition & 1 deletion openbb_terminal/core/plots/web/bar_menus.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ function autoScaling(eventdata, graphs) {

if (is_volume) {
if (graphs.layout[yaxis].tickvals != undefined) {
const range_x = 4;
const range_x = 7;
let volume_ticks = org_y_max;
let round_digits = -3;
let first_val = Math.round(volume_ticks * 0.2, round_digits);
Expand Down
74 changes: 15 additions & 59 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ mstarpy = "^0.0.4"
packaging = ">=22.0"
rapidfuzz = "^2.13.7"
streamlit = "^1.17.0"
pywry = "^0.3.6"
pywry = "^0.3.7"
svglib = "^1.5.0"
sparqlwrapper = "^2.0.0"
y-py = "!=0.5.5" # Untraceable third level dependency that requires cargo to be built
Expand Down