Plotly plot templating problem

Can you please help me why seaborn image goes fine in my html template but not plotly plot? When I save the output in my computer as html, both images work and I can manually change it to pdf; but I want my streamlit app to do convert the html to pdf on the app.

import pdfkit
path_wkhtmltopdf = r’C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe’
config = pdfkit.configuration(wkhtmltopdf=path_wkhtmltopdf)
import os
import pandas as pd
import numpy as np
from jinja2 import Environment, PackageLoader, select_autoescape, FileSystemLoader
from datetime import date
import streamlit as st
from PIL import Image
import base64
from io import StringIO, BytesIO
import io
import matplotlib.pyplot as plt
import plotly.express as px
import seaborn as sbn
import plotly.io as pio
import subprocess

st.set_page_config(layout=“centered”, page_icon=“”, page_title=“PDF Report Generator for News HW01”)
st.title(“PDF generation with plotly”)

Load HTML template

env = Environment(loader=FileSystemLoader(“.”), autoescape=select_autoescape())
template = env.from_string(“”"

Title Full Name:{{username }}




{{ img02_html|safe }}
""") # here the template is directly in the main .py file which is not ideal st.write("Instruction here")

form = st.form(“template_form”) #form initiated
username = form.text_input(“Full NAME”)

X = [1 , 10]
Y = [1 , 20]
sbn.scatterplot(x=X , y=Y)

Create img01

img01 = io.BytesIO()
plt.savefig(img01, format=“png”)
img01.seek(0) # Reset the stream position to the beginning
img01=base64.b64encode(img01.read()).decode(“utf-8”) , # Encode and pass as base64

df = px.data.iris()
img02 = px.scatter(df, x=“sepal_width”, y=“sepal_length”, height=400, width=600, color=“species”, color_discrete_sequence= [
#0068c9”,
#83c9ff”,
#ff2b2b”,
] ,
title= “New title is here”,
)

Generate the HTML code for the Plotly plot

img02_html = pio.to_html(img02, include_plotlyjs=‘cdn’, full_html=False)

submit = form.form_submit_button(“Generate PDF”) #submit button

if submit:
# Render the HTML template
rendered_html = template.render(
username=username,
img01=img01, # for seaborn plot
img02_html=img02_html, # for plotly plot from interactivity
)
output_file_path = ‘./output3.html’
with open(output_file_path , ‘w’) as file:
file.write(rendered_html)

pdf = pdfkit.from_string(rendered_html, configuration=config)


st.download_button( # Download button to prompt the user to download the generated pdf file
    "⬇️ Download pdf",
    data=pdf,
    file_name="pdf_output.pdf",
    mime="application/octet-stream",
)

Hey @data.student ,

I don’t believe your requested feature is currently available on Streamlit.

Id’ suggest you try using this: pdftotree · PyPI

It should be able to convert files and satisfy your requested functionality

Let me know if it works!