MCPcopy Create free account
hub / github.com/streamlit/streamlit / _navigation

Function _navigation

lib/streamlit/commands/navigation.py:332–494  ·  view source on GitHub ↗
(
    pages: Sequence[PageType] | Mapping[SectionHeader, Sequence[PageType]],
    *,
    position: Literal["sidebar", "hidden", "top"],
    expanded: bool | int,
)

Source from the content-addressed store, hash-verified

330
331
332def _navigation(
333 pages: Sequence[PageType] | Mapping[SectionHeader, Sequence[PageType]],
334 *,
335 position: Literal["sidebar", "hidden", "top"],
336 expanded: bool | int,
337) -> StreamlitPage:
338 if isinstance(pages, Sequence):
339 converted_pages = [convert_to_streamlit_page(p) for p in pages]
340 nav_sections = {"": converted_pages}
341 else:
342 nav_sections = {
343 section: [convert_to_streamlit_page(p) for p in section_pages]
344 for section, section_pages in pages.items()
345 }
346 page_list = pages_from_nav_sections(nav_sections)
347
348 if not page_list:
349 raise StreamlitAPIException(
350 "`st.navigation` must be called with at least one `st.Page`."
351 )
352
353 default_page = None
354 pagehash_to_pageinfo: dict[PageHash, PageInfo] = {}
355
356 # Get the default page.
357 for section_header in nav_sections:
358 for page in nav_sections[section_header]:
359 if page._default:
360 if default_page is not None:
361 raise StreamlitAPIException(
362 "Multiple Pages specified with `default=True`. "
363 "At most one Page can be set to default."
364 )
365 default_page = page
366
367 if default_page is None:
368 non_external_pages = [p for p in page_list if not p.is_external]
369 if not non_external_pages:
370 raise StreamlitAPIException(
371 "At least one non-external page is required. "
372 "External URL pages cannot be the default page."
373 )
374 default_page = non_external_pages[0]
375 default_page._default = True
376
377 ctx = get_script_run_ctx()
378 if not ctx:
379 # This should never run in Streamlit, but we want to make sure that
380 # the function always returns a page
381 default_page._can_be_called = True
382 return default_page
383
384 # Build the pagehash-to-pageinfo mapping.
385 for section_header in nav_sections:
386 for page in nav_sections[section_header]:
387 script_path = str(page._page) if isinstance(page._page, Path) else ""
388
389 script_hash = page._script_hash

Callers 2

_mpa_v1Function · 0.90
navigationFunction · 0.85

Calls 14

get_script_run_ctxFunction · 0.90
is_emojiFunction · 0.90
pages_from_nav_sectionsFunction · 0.85
_set_external_urlFunction · 0.85
send_page_not_foundFunction · 0.85
addMethod · 0.80
set_pagesMethod · 0.80
get_page_scriptMethod · 0.80
set_mpa_v2_pageMethod · 0.80
itemsMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…