Skip to content

Commit a5eda94

Browse files
ethanwharriscarmocca
authored andcommitted
[App] Fix support for streamlit > 1.14 (#16139)
1 parent accfbc1 commit a5eda94

File tree

2 files changed

+31
-12
lines changed

2 files changed

+31
-12
lines changed

src/lightning_app/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
6666
- Fixed the endpoint info tab not showing up in `AutoScaler` UI ([#16128](https://github.com/Lightning-AI/lightning/pull/16128))
6767

6868

69+
- Fixed an issue where an exception would be raised in the logs when using a recent version of streamlit ([#16139](https://github.com/Lightning-AI/lightning/pull/16139))
70+
71+
6972
## [1.8.5] - 2022-12-15
7073

7174
### Added

src/lightning_app/utilities/app_helpers.py

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -184,11 +184,22 @@ def render_non_authorized(self):
184184

185185

186186
def target_fn():
187-
from streamlit.server.server import Server
187+
try:
188+
# streamlit >= 1.14.0
189+
from streamlit import runtime
190+
191+
get_instance = runtime.get_instance
192+
exists = runtime.exists()
193+
except ImportError:
194+
# Older versions
195+
from streamlit.server.server import Server
196+
197+
get_instance = Server.get_current
198+
exists = bool(Server._singleton)
188199

189200
async def update_fn():
190-
server = Server.get_current()
191-
sessions = list(server._session_info_by_id.values())
201+
runtime_instance = get_instance()
202+
sessions = list(runtime_instance._session_info_by_id.values())
192203
url = (
193204
"localhost:8080"
194205
if "LIGHTNING_APP_STATE_URL" in os.environ
@@ -198,15 +209,20 @@ async def update_fn():
198209
last_updated = time.time()
199210
async with websockets.connect(ws_url) as websocket:
200211
while True:
201-
_ = await websocket.recv()
202-
while (time.time() - last_updated) < 1:
203-
time.sleep(0.1)
204-
for session in sessions:
205-
session = session.session
206-
session.request_rerun(session._client_state)
207-
last_updated = time.time()
208-
209-
if Server._singleton:
212+
try:
213+
_ = await websocket.recv()
214+
215+
while (time.time() - last_updated) < 1:
216+
time.sleep(0.1)
217+
for session in sessions:
218+
session = session.session
219+
session.request_rerun(session._client_state)
220+
last_updated = time.time()
221+
except websockets.exceptions.ConnectionClosedOK:
222+
# The websocket is not enabled
223+
break
224+
225+
if exists:
210226
asyncio.run(update_fn())
211227

212228

0 commit comments

Comments
 (0)