Skip to content

Commit

Permalink
Fix dashboard_run to take an App object (#40)
Browse files Browse the repository at this point in the history
* fix for published experience

* added dash app changes

* bug fix

* validate root path

* bug fix

* get base path from env var

* changed app.run_server -> app.run

* removed unused imports

* fixed lint errors

* improved error message
  • Loading branch information
srinathnarayanan authored Oct 16, 2024
1 parent 6f07110 commit 86c4d8d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 22 deletions.
4 changes: 1 addition & 3 deletions singlestoredb/apps/_cloud_functions.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import asyncio
import textwrap
import typing
import urllib.parse

from ._config import AppConfig
from ._connection_info import ConnectionInfo
Expand Down Expand Up @@ -53,8 +52,7 @@ async def run_function_app(
def ping() -> str:
return 'Success!'

base_path = urllib.parse.urlparse(app_config.base_url).path
app.root_path = base_path
app.root_path = app_config.base_path

config = uvicorn.Config(
app,
Expand Down
3 changes: 3 additions & 0 deletions singlestoredb/apps/_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
class AppConfig:
listen_port: int
base_url: str
base_path: str
app_token: Optional[str]
user_token: Optional[str]
running_interactively: bool
Expand All @@ -26,6 +27,7 @@ def _read_variable(name: str) -> str:
def from_env(cls) -> 'AppConfig':
port = cls._read_variable('SINGLESTOREDB_APP_LISTEN_PORT')
base_url = cls._read_variable('SINGLESTOREDB_APP_BASE_URL')
base_path = cls._read_variable('SINGLESTOREDB_APP_BASE_PATH')

workload_type = os.environ.get('SINGLESTOREDB_WORKLOAD_TYPE')
running_interactively = workload_type == 'InteractiveNotebook'
Expand All @@ -46,6 +48,7 @@ def from_env(cls) -> 'AppConfig':
return cls(
listen_port=int(port),
base_url=base_url,
base_path=base_path,
app_token=app_token,
user_token=user_token,
running_interactively=running_interactively,
Expand Down
30 changes: 11 additions & 19 deletions singlestoredb/apps/_dashboards.py
Original file line number Diff line number Diff line change
@@ -1,46 +1,38 @@
import typing
import urllib.parse

from ._config import AppConfig
from ._process import kill_process_by_port
from ._stdout_supress import StdoutSuppressor
from singlestoredb.apps._connection_info import ConnectionInfo

if typing.TYPE_CHECKING:
from plotly.graph_objs import Figure
from dash import Dash


async def run_dashboard_app(
figure: 'Figure',
app: 'Dash',
debug: bool = False,
kill_existing_app_server: bool = True,
) -> ConnectionInfo:
try:
import dash
from dash import Dash
except ImportError:
raise ImportError('package dash is required to run dashboards')

try:
from plotly.graph_objs import Figure
except ImportError:
raise ImportError('package dash is required to run dashboards')

if not isinstance(figure, Figure):
raise TypeError('figure is not an instance of plotly Figure')
if not isinstance(app, Dash):
raise TypeError('app is not an instance of Dash App')

app_config = AppConfig.from_env()

if kill_existing_app_server:
kill_process_by_port(app_config.listen_port)

base_path = urllib.parse.urlparse(app_config.base_url).path

app = dash.Dash(requests_pathname_prefix=base_path)
app.layout = dash.html.Div(
[
dash.dcc.Graph(figure=figure),
],
)
if app.config.requests_pathname_prefix is None or \
app.config.requests_pathname_prefix != app_config.base_path:
raise RuntimeError('''
requests_pathname_prefix of the Dash App is invalid. Please set
requests_pathname_prefix=os.environ['SINGLESTOREDB_APP_BASE_PATH']
while initializing the Dash App and retry''')

with StdoutSuppressor():
app.run(
Expand Down

0 comments on commit 86c4d8d

Please sign in to comment.