OrderedDict

Hello Good people, please help me here
I am trying to implement an OrderedDict wiht function calls to call other pages when selected from drop down menu of the sidebar just the way DEMOS work.

Hi @wilson_uzziel -

Have you seen this code for the hello demo, where is shows how we’re using the ordered dict?

As a small note you can likely just use a dictionary now. As part of the spec for 3.7 and part of the implementation of 3.6 regular dictionaries keep the order items were inserted.

1 Like

Thanks @randyzwitch i have seen this but the problem is how to implement it on a new project because it brings and error

Hello @wilson_uzziel,

What error do you encounter?
Here’s an example of a page system using OrderedDict, a little bit different from streamlit’s hello sample. However, I suggest you use regular python dictionaries anyway.

import streamlit as st
from collections import OrderedDict


def main():
    pages = OrderedDict([
        ("Hello", page_hello),
        ("World", page_world),
    ])
    
    st.sidebar.title(":closed_book: Page sample")
    page = st.sidebar.selectbox("Browse page.", list(pages.keys()))

    pages[page]()


def page_hello():
    st.title("Hello")
    st.write("First page content.")


def page_world():
    st.title("World")
    st.write("Second page content.")


if __name__ == "__main__":
    main()

Thanks @Synode, I am encountering UnboundLocalError: local variable ‘page_hello’ referenced before assignment. Pliz help

I suppose you tried to write your code at the top level of your script. In such case, function definitions must be set before the ordered dict, or in separate python files you would import, just like the demo.

Here is another example without the main() function.

import streamlit as st
from collections import OrderedDict

def page_hello():
    st.title("Hello")
    st.write("First page content.")

def page_world():
    st.title("World")
    st.write("Second page content.")

pages = OrderedDict([
    ("Hello", page_hello),
    ("World", page_world),
])
    
st.sidebar.title(":closed_book: Page sample")
page = st.sidebar.selectbox("Browse page.", list(pages.keys()))

pages[page]()

Hi @Synode, this worked really fine no errors

Hi @Synode, now suppose am getting my pages from data frame that is CSV file. Such that pages are populate from csv file then on every page i go ahead and filter data