With the release of the page_link widget there is a new approach to building a navigation bar without renaming your current file names.
Here is an example.
-
Set the showSidebarNavigation to false, this would prevent showing the multi-pages in the sidebar.
- create a .streamlit folder
- create a config.toml file, and write
showSidebarNavigation = false
-
In your streamlit_app.py
import streamlit as st
def main():
# builds the sidebar menu
with st.sidebar:
st.page_link('streamlit_app.py', label='Individual Checker', icon='🔥')
st.page_link('pages/competition.py', label='Competition Checker', icon='🛡️')
st.title(f'🔥 Individual Checker')
# your content
if __name__ == '__main__':
main()
- In your pages, my example just uses
competition.py
, this could be different in your case. But there is no need to rename it. See the page_link parameters below.
import streamlit as st
def main():
# builds the sidebar menu
with st.sidebar:
st.page_link('streamlit_app.py', label='Individual Checker', icon='🔥')
st.page_link('pages/competition.py', label='Competition Checker', icon='🛡️')
st.title(f'🛡️ Competition Checker')
# your content
if __name__ == '__main__':
main()
The navigation menu is just like this.
with st.sidebar:
st.page_link('streamlit_app.py', label='Individual Checker', icon='🔥')
st.page_link('pages/competition.py', label='Competition Checker', icon='🛡️')
Code improvement
You can create a function and put that on it and call that function in any page you want it to be.
For example you can create a folder called modules under your app folder name.
modules/nav.py
import streamlit as st
def Navbar():
with st.sidebar:
st.page_link('streamlit_app.py', label='Individual Checker', icon='🔥')
st.page_link('pages/competition.py', label='Competition Checker', icon='🛡️')
And import that Navbar component in other pages you have.
streamlit_app.py
import streamlit as st
from modules.nav import Navbar
def main():
Navbar()
st.title(f'🔥 Individual Checker')
if __name__ == '__main__':
main()
pages/competition.py
import streamlit as st
from modules.nav import Navbar
def main():
Navbar()
st.title(f'🛡️ Competition Checker')
if __name__ == '__main__':
main()
Reference
Custom Navigation