How to Print Streamlit App Pages into a PDF file?
Asked Answered
D

1

0

This simple example below I created with help of @MathCatsAnd. There are multiple dataframes in each page, and only one dataframe per page is shown in the example below for simplicity. I would like to be able to print each page (with multiple dataframes) to a pdf file in standard 11x8.5 paper size. I would want to be able to fit the 26 columns in the paper width-wise, and values length-wise to be printed in multiple pages as the next one being continuation of previous one. I don't know why streamlit is lacking on this basic thing.

import streamlit as st
import pandas as pd
import numpy as np

# Initialize session state with dataframes
# Include initialization of "edited" slots by copying originals
if 'df1' not in st.session_state:
    st.title("**:blue[Title123]**")
    Nvals = 200
    rows = ['a{}'.format(i) for i in range(1, Nvals+1)] # a1:a19
    values = np.arange(Nvals)
    st.session_state.df1 = pd.DataFrame({
        "rows/cols": rows, 
        'A': list(range(Nvals)), 'B': list(range(Nvals)), 'C': list(range(Nvals)), 'D': list(range(Nvals)), 'E': list(range(Nvals)),
        'F': list(range(Nvals)), 'G': list(range(Nvals)), 'H': list(range(Nvals)), 'I': list(range(Nvals)), 'J': list(range(Nvals)),
        'K': list(range(Nvals)), 'L': list(range(Nvals)), 'M': list(range(Nvals)), 'N': list(range(Nvals)), 'Q': list(range(Nvals)),
        'O': list(range(Nvals)), 'P': list(range(Nvals)), 'R': list(range(Nvals)), 'S': list(range(Nvals)), 'T': list(range(Nvals)),
        'U': list(range(Nvals)), 'V': list(range(Nvals)), 'W': list(range(Nvals)), 'X': list(range(Nvals)), 'Y': list(range(Nvals)), 'Z': list(range(Nvals)),
    })
    st.session_state.edited_df1 = st.session_state.df1.copy()
    #
    st.session_state.df2 = pd.DataFrame({
        "col1": ["b1", "b2", "b3", "b4", "b5", "b6", "b7", "b8", "b9", "b10"], 
        "Values": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
        "col2": ["b1", "b2", "b3", "b4", "b5", "b6", "b7", "b8", "b9", "b10"], 
        "Values": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
        "col3": ["b1", "b2", "b3", "b4", "b5", "b6", "b7", "b8", "b9", "b10"], 
        "Values": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
    })
    st.session_state.edited_df2 = st.session_state.df2.copy()

# Save edits by copying edited dataframes to "original" slots in session state
def on_change_callback_fcn():
    st.session_state.df1 = st.session_state.edited_df1 
    st.session_state.df2 = st.session_state.edited_df2

# Sidebar to select page and commit changes upon selection
page = st.sidebar.selectbox("Select: ", ("A","B"), on_change=on_change_callback_fcn, key='SelectedSelectboxVal') 
# Convenient shorthand notation used below in funct1 and funct2
def funct1():
    st.session_state.edited_df1 = st.data_editor(st.session_state.df1, num_rows="dynamic",height=10*len(st.session_state.edited_df1)) 

def funct2():
    st.session_state.edited_df2 = st.data_editor(st.session_state.df2, num_rows="dynamic")

if  page == "A":
    st.header("Page A")
    funct1()
elif page == "B":
    st.header("Page B")
    funct2()

print('\n ...................... \n Entire Code Ran \n ...................... \n')
print(len(st.session_state.edited_df1))

I couldn't find many materials online regarding this. Ctrl+P doesn't print properly. How can this be done?

Die answered 7/6, 2023 at 2:52 Comment(1)
There is some limited support for printing via the hamburger menu in the upper right of your app. It's not gloriously beautiful as of June 2023 but it's what is currently available within Streamlit. Otherwise, you could try to capture the web page through browser methods, such as Right Click -> Take a Screenshot in Firefox. (May see Right Click ->This Frame -> Take a Screenshot in a Streamlit app...)Saviour
I
0

There is already a great example of a streamlit app doing this.

This is the link to its GitHub: https://github.com/H-A-Dye/DataQuilt

But it does create the functionality from scratch, rather than using a built-in.

Inattention answered 12/6, 2023 at 14:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.