Skip to content

Commit 9dce034

Browse files
ethanwharrislantiga
authored andcommitted
App: Fix AppState, streamlit example (#17452)
(cherry picked from commit e826724)
1 parent 01d2d55 commit 9dce034

File tree

4 files changed

+48
-15
lines changed

4 files changed

+48
-15
lines changed

.azure/app-cloud-e2e.yml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,9 @@ jobs:
7070
'App: boring_app':
7171
name: "boring_app"
7272
dir: "public"
73-
# TODO: RESOLVE ME ASAP
74-
# 'App: template_streamlit_ui':
75-
# name: "template_streamlit_ui"
76-
# dir: "public"
73+
'App: template_streamlit_ui':
74+
name: "template_streamlit_ui"
75+
dir: "public"
7776
'App: template_react_ui':
7877
name: "template_react_ui"
7978
dir: "public"

requirements/app/base.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
lightning-cloud >=0.5.33
1+
lightning-cloud >=0.5.34
22
packaging
33
typing-extensions >=4.0.0, <=4.4.0
44
deepdiff >=5.7.0, <6.2.4

src/lightning/app/utilities/state.py

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
from lightning.app.core.constants import APP_SERVER_HOST, APP_SERVER_PORT
2727
from lightning.app.storage.drive import _maybe_create_drive
2828
from lightning.app.utilities.app_helpers import AppStatePlugin, BaseStatePlugin, Logger
29-
from lightning.app.utilities.network import _configure_session
29+
from lightning.app.utilities.network import _configure_session, LightningClient
3030

3131
logger = Logger(__name__)
3232

@@ -50,6 +50,7 @@ def headers_for(context: Dict[str, str]) -> Dict[str, str]:
5050

5151
class AppState:
5252
_APP_PRIVATE_KEYS: Tuple[str, ...] = (
53+
"_use_localhost",
5354
"_host",
5455
"_session_id",
5556
"_state",
@@ -93,10 +94,9 @@ def __init__(
9394
on this AppState, this affiliation will be used to reduce the scope of the given state.
9495
plugin: A plugin to handle authorization.
9596
"""
96-
use_localhost = "LIGHTNING_APP_STATE_URL" not in os.environ
97-
self._host = host or APP_SERVER_HOST
98-
self._port = port or (APP_SERVER_PORT if use_localhost else None)
99-
self._url = f"{self._host}:{self._port}" if use_localhost else self._host
97+
self._use_localhost = "LIGHTNING_APP_STATE_URL" not in os.environ
98+
self._host = host or ("http://127.0.0.1" if self._use_localhost else None)
99+
self._port = port or (APP_SERVER_PORT if self._use_localhost else None)
100100
self._last_state = last_state
101101
self._state = state
102102
self._session_id = "1234"
@@ -105,6 +105,23 @@ def __init__(
105105
self._attach_plugin(plugin)
106106
self._session = self._configure_session()
107107

108+
@property
109+
def _url(self) -> str:
110+
if self._host is None:
111+
app_ip = ""
112+
113+
if "LIGHTNING_CLOUD_PROJECT_ID" in os.environ and "LIGHTNING_CLOUD_APP_ID" in os.environ:
114+
client = LightningClient()
115+
app_instance = client.lightningapp_instance_service_get_lightningapp_instance(
116+
os.environ.get("LIGHTNING_CLOUD_PROJECT_ID"),
117+
os.environ.get("LIGHTNING_CLOUD_APP_ID"),
118+
)
119+
app_ip = app_instance.status.ip_address
120+
121+
# TODO: Don't hard code port 8080 here
122+
self._host = f"http://{app_ip}:8080" if app_ip else APP_SERVER_HOST
123+
return f"{self._host}:{self._port}" if self._use_localhost else self._host
124+
108125
def _attach_plugin(self, plugin: Optional[BaseStatePlugin]) -> None:
109126
if plugin is not None:
110127
plugin = plugin

tests/tests_app/utilities/test_state.py

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import pytest
66
import requests
7+
from lightning_cloud.openapi import Externalv1LightningappInstance, V1LightningappInstanceStatus
78

89
import lightning.app
910
from lightning.app import LightningApp, LightningFlow, LightningWork
@@ -266,13 +267,29 @@ def test_get_send_request(monkeypatch):
266267
state.w.counter = 1
267268

268269

269-
@mock.patch("lightning.app.utilities.state.APP_SERVER_HOST", "https://lightning-cloud.com")
270-
@mock.patch.dict(os.environ, {"LIGHTNING_APP_STATE_URL": "https://lightning-cloud.com"})
271-
def test_app_state_with_env_var(**__):
270+
@mock.patch.dict(
271+
os.environ,
272+
{
273+
"LIGHTNING_APP_STATE_URL": "https://lightning-cloud.com",
274+
"LIGHTNING_CLOUD_PROJECT_ID": "test-project-id",
275+
"LIGHTNING_CLOUD_APP_ID": "test-app-id",
276+
},
277+
)
278+
@mock.patch("lightning.app.utilities.state.LightningClient")
279+
def test_app_state_with_env_var(mock_client):
280+
mock_client().lightningapp_instance_service_get_lightningapp_instance.return_value = Externalv1LightningappInstance(
281+
status=V1LightningappInstanceStatus(ip_address="test-ip"),
282+
)
272283
state = AppState()
273-
assert state._host == "https://lightning-cloud.com"
284+
url = state._url
285+
286+
mock_client().lightningapp_instance_service_get_lightningapp_instance.assert_called_once_with(
287+
"test-project-id",
288+
"test-app-id",
289+
)
290+
291+
assert url == "http://test-ip:8080"
274292
assert not state._port
275-
assert state._url == "https://lightning-cloud.com"
276293

277294

278295
@mock.patch.dict(os.environ, {})

0 commit comments

Comments
 (0)