2626from lightning .app .core .constants import APP_SERVER_HOST , APP_SERVER_PORT
2727from lightning .app .storage .drive import _maybe_create_drive
2828from 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
3131logger = Logger (__name__ )
3232
@@ -50,6 +50,7 @@ def headers_for(context: Dict[str, str]) -> Dict[str, str]:
5050
5151class 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
0 commit comments