I have a Flask web app with a download button. When I click on the button I would like to execute a python method, not a javascript method. This python method should take as input some recently modified html code from the web page.
Most precisely the following happens: the user is asked to fill the blanks in some text and when she is done clicks on the download button. When the button gets clicked, a python function render_pdf()
is executed that converts the html modified text to a pdf by using pdfkit
library.
In its most simple version, my project has the following structure:
- static/sample.pdf
- templates/index.html
- main.py
And this is the code I have so far:
main.py
from flask import Flask, render_template
import pdfkit
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
if __name__ == "__main__":
app.run(debug=True)
index.html
<body>
<p class="test">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Temporibus, beatae!
<span contenteditable="true" class="badge alert-info name" data-placeholder="Enter your name" data-focused-advice="Start typing"></span><i class="fa fa-lg fa-plus-circle"></i>
Lorem ipsum dolor sit, amet consectetur adipisicing elit. Perspiciatis, laboriosam?
</p>
<button onclick="render_pdf()"><a href="../static/sample.pdf" download="my_file">Download</a></button>
</body>
The python method I would like to execute should be this one, passing htmlstr
as input:
import pdfkit
def render_pdf(htmlstr):
pdfkit.from_string(htmlstr, './static/sample.pdf')
I know I can get with javascript the variable htmlstr
by doing document.getElementsByClassName("test")[0].innerHTML;
I feel I am super close to get there and I have checked maybe a bit old but very similar questions but I just do not know how to pass the input html to the python method. What am I missing?