簡體   English   中英

有沒有辦法從 Streamlit 應用程序提高 nosql db 的負載?

[英]Is there a way to improve the load of nosql db from Streamlit app?

我正在編寫一個 streamlit 應用程序,我從 nosql 數據庫中讀取數據我需要找到我收集的數據的所有值 (26000) 我在運行應用程序時通過 pymongo 查詢來執行此操作。

很好....我已經嘗試了 2000 多行並且我沒有問題,當我像上面所說的那樣更改集合時,streamlit 在循環中重新運行並且 web 套接字引發錯誤。

有沒有辦法從數據庫中獲取大量數據並將其在 streamlit 應用程序上表示為 dataframe 而不會引發異常?

我通過修改server.maxMessageSize的配置設置增加了消息的大小,但仍然不起作用

如果您至少可以包含錯誤消息本身以幫助縮小特定問題的范圍,這可能會有所幫助。 不過,據我所知,最重要的問題似乎是您嘗試向 Streamlit 發送的數據超過其最大消息大小值所允許的數據。 我假設“我增加了消息的大小”意味着您已嘗試根據Streamlit 配置指南修改server.maxMessageSize的配置設置,但如果不是這種情況,那么您應該嘗試一下。

由於您擁有大量數據,最好嘗試使用某種形式的分頁。 也就是說:不是將整個 dataframe 發送到 Streamlit,而是發送它的一部分(“頁面”)並提供某種在“頁面”之間切換的方式,例如以“上一頁”/“下一頁”的形式頁”按鈕。 例如,假設您將數據加載為 Pandas dataframe 並將 Streamlit 導入為st ,您可以嘗試將分頁封裝到 function 中,如下所示:

def paginate_dataframe(df, page_size: int):
    # Determine the maximum number of pages we should experience
    from math import ceil
    n_pages = ceil(len(df)/page_size)
    last_page = n_pages - 1

    # Get current page, initializing the state of the value if necessary
    current_page = st.session_state.setdefault("current_page", 0)

    # Define callback functions for the previous/next buttons so they can adjust the current page
    def decrement_page():
        if st.session_state.current_page > 0:
            st.session_state.current_page -= 1

    def increment_page():
        if st.session_state.current_page < last_page:
            st.session_state.current_page += 1

    # Create previous/next buttons which will be disabled when on the first or last pages, respectively
    button_columns = st.columns(2)

    with button_columns[0]:
        st.button(
            label="previous page",
            on_click=decrement_page,
            disabled=(current_page <= 0),
        )

    with button_columns[1]:
        st.button(
            label="next page",
            on_click=increment_page,
            disabled=(current_page >= last_page),
        )

    page_start = current_page * page_size
    st.dataframe(df.iloc[page_start: page_start+page_size])

paginate_dataframe(df, page_size=5)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM