Skip to content

Commit

Permalink
[Feature] PyWry Optional With Charting (#6590)
Browse files Browse the repository at this point in the history
* pywry optional

* plot_open_export

* black

* unused imports

* remove commented lines
  • Loading branch information
deeleeramone authored Jul 29, 2024
1 parent 558fe26 commit 586df54
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 408 deletions.
207 changes: 29 additions & 178 deletions cli/poetry.lock

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion cli/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ prompt-toolkit = "^3.0.16"
rich = "^13"
python-dotenv = "^1.0.0"
openpyxl = "^3.1.2"
pywry = "^0.6.2"

[build-system]
requires = ["setuptools<65.5.0", "poetry-core>=1.0.0"]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,6 @@ def __init__(
self.debug_mode: bool = system_settings.debug_mode or Env().DEBUG_MODE
self.headless: bool = system_settings.headless
# User
self.plot_enable_pywry: bool = user_settings.preferences.plot_enable_pywry
self.plot_pywry_width: int = user_settings.preferences.plot_pywry_width
self.plot_pywry_height: int = user_settings.preferences.plot_pywry_height
self.plot_open_export: bool = user_settings.preferences.plot_open_export
self.user_email: Optional[str] = getattr(
user_settings.profile.hub_session, "email", None
)
Expand Down
6 changes: 0 additions & 6 deletions openbb_platform/core/openbb_core/app/model/preferences.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,6 @@ class Preferences(BaseModel):
description="Python default output type.",
validate_default=True,
)
plot_enable_pywry: bool = True
plot_open_export: bool = (
False # Whether to open plot image exports after they are created
)
plot_pywry_height: PositiveInt = 762
plot_pywry_width: PositiveInt = 1400
request_timeout: PositiveInt = 60
show_warnings: bool = True
table_style: Literal["dark", "light"] = "dark"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
from pathlib import Path
from typing import TYPE_CHECKING, Any, Dict, Optional, Union

from openbb_core.env import Env

if TYPE_CHECKING:
from openbb_core.app.model.charts.charting_settings import ChartingSettings
from pandas import DataFrame
Expand All @@ -16,9 +14,7 @@

try:
from pywry import PyWry # pylint: disable=import-outside-toplevel
except ImportError as e:
if Env().DEBUG_MODE:
print(f"\033[91m{e}\033[0m") # noqa: T201
except ImportError:
from .dummy_backend import DummyBackend # pylint: disable=import-outside-toplevel

class PyWry(DummyBackend): # type: ignore
Expand Down Expand Up @@ -89,8 +85,8 @@ def __init__(

def set_window_dimensions(self):
"""Set the window dimensions."""
width = self.charting_settings.plot_pywry_width or 1400
height = self.charting_settings.plot_pywry_height or 762
width = 1400
height = 762

self.WIDTH, self.HEIGHT = int(width), int(height)

Expand Down Expand Up @@ -233,7 +229,6 @@ async def process_image(self, export_image: Path):
"""Check if the image has been exported to the path."""
# pylint: disable=import-outside-toplevel
import asyncio
import os
import subprocess
import sys

Expand All @@ -247,14 +242,8 @@ async def process_image(self, export_image: Path):
break

if img_path.exists(): # noqa: SIM102
if self.charting_settings.plot_open_export:
if sys.platform == "win32":
os.startfile(export_image) # nosec: B606 # noqa: S606
else:
opener = "open" if sys.platform == "darwin" else "xdg-open"
subprocess.check_call(
[opener, export_image] # nosec: B603 # noqa: S603
)
opener = "open" if sys.platform == "darwin" else "xdg-open"
subprocess.check_call([opener, export_image]) # nosec: B603 # noqa: S603

def send_table(
self,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,37 +1,14 @@
"""Dummy backend for charting to avoid import errors."""

import asyncio
import warnings
from queue import Queue
from typing import List

import dotenv
from openbb_core.app.constants import OPENBB_DIRECTORY
from openbb_core.env import Env

SETTINGS_ENV_FILE = OPENBB_DIRECTORY / ".env"

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[/]
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[/]\n
[yellow]Debian / Ubuntu:[/]
[green]sudo apt install libwebkit2gtk-4.0-dev[/]\n
[yellow]Fedora / CentOS / AlmaLinux:[/]
[green]sudo dnf install gtk3-devel webkit2gtk3-devel[/]\r
"""


class DummyBackend:
"""Dummy class to avoid import errors."""
Expand Down Expand Up @@ -63,8 +40,6 @@ def __init__(self, daemon: bool = True, max_retries: int = 30):
self.loop = asyncio.new_event_loop()
asyncio.set_event_loop(self.loop)

if Env().DEBUG_MODE:
warnings.warn(pywry_missing)
dotenv.set_key(SETTINGS_ENV_FILE, "PLOT_ENABLE_PYWRY", "0")

def close(self, reset: bool = False): # pylint: disable=W0613
Expand Down
Loading

0 comments on commit 586df54

Please sign in to comment.