I have a Streamlit app that mimics daily reports. Where each input widget is initialized with a value from the database.
I am trying to export/save my Streamlit app as a pdf and or screenshot.
At the moment, I am trying to create multiple screenshots of the page and then combine them into single full screenshot. To do so I need to move my window position by x amount.
I have tried the following script using Selenium,however scroll action doesn’t happen/work. I have also tried options with PAGE_DOWN Key.
Please suggest what could be the issue and potential solution.
Steps to reproduce
Code snippet:
driver = webdriver.Chrome()
# Navigate to the desired web page
url =' http://localhost:8501/'
driver.get(url)
scroll_x = 0 # Horizontal scroll position
scroll_y = 500 # Vertical scroll position
driver.execute_script(f"window.scrollTo({scroll_x}, {scroll_y});")
You should be able to use the menu in the top right corner and choose “print”
If you want to do it automatically, and selenium isn’t working for you, you might try using Playwright, which supports full page screenshots. Screenshots | Playwright
Thank you for your fast response. I am able to use “print” option in the top right corner. However, It produce inconsistent results, as it based on the screen resolution. Moreover, it does produce some blank pages at the start.
I have tested Playwright. Using following script, but it didn’t managed to produce full page (i.e. screenshot_full.png==screenshot.png), however it worked for some arbitrary website.
Hi Kawanovs,
Please visit the below youtube video. It uses headless option and lambda function.
Selenium Full Page ScreenShot Youtube Video : https://www.youtube.com/watch?v=iOpGGW__oz4
Thank you for your suggestion. It does work work some arbitrary website, but not for the streamlit app.
With the given code I get following error:
from selenium.webdriver.chrome.service import Service
chrome_options = Options()
chrome_options.add_argument("--headless") # Set any desired options
driver = webdriver.Chrome(options=chrome_options, service=Service(ChromeDriverManager().install()))
driver.implicitly_wait(10)
# Navigate to the desired web page
url = ' http://localhost:8501/' # Replace with your desired URL
driver.get(url)
# Small screenshot
driver.get_screenshot_as_file('report.png')
# Full screenshot
S = lambda x: driver.execute_script('return document.body.parentNode.scroll' + x)
driver.set_window_size(S('Width'), S('Height'))
driver.find_element(By.XPATH, '/html/body').screenshot('report_full.png')
selenium.common.exceptions.WebDriverException: Message: unknown error: unhandled inspector error: {“code”:-32000,“message”:“Cannot take screenshot with 0 height.”}. I have also tried to use other elements, but I only have managed to get a small screenshot.
Any other suggestion are appreciated!
UPDATE:
Following solution was found. It does produce some bugs with sidebar animation and loading animation, but nothing critical.
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.