Forums

WeasyPrint - render_pdf module not importing

I am trying to get into my web app to drop and recreate the database tables, but after updating some code to include a download to pdf feature using weasyprint, I am unable to even import the db, and class elements in BASH.

Attached are screenshots of the code being imported and error in the BASH.

enter image description here enter image description here

You need to do a 'pip install'

Hey Conrad, I have already run "pip install weasyprrint". It says the module it updated and loaded, where I am having trouble is the error says that it can not import render_pdf.

Any thoughts?

which version of python are you running?

15:29 ~/mysite (pythonanywhere)$ python -V Python 2.7.6

This is the message I got when I tried pip install --user WeasyPrint

15:35 ~/mysite (pythonanywhere)$ pip install WeasyPrint
Requirement already satisfied (use --upgrade to upgrade): WeasyPrint in /usr/local/lib/python2.7/dist-packa
ges
Requirement already satisfied (use --upgrade to upgrade): lxml>=3.0 in /usr/local/lib/python2.7/dist-packag
es (from WeasyPrint)
Requirement already satisfied (use --upgrade to upgrade): cssselect>=0.6 in /usr/local/lib/python2.7/dist-p
ackages (from WeasyPrint)
Requirement already satisfied (use --upgrade to upgrade): CairoSVG<2,>=1.0.20 in /usr/local/lib/python2.7/d
ist-packages (from WeasyPrint)
Requirement already satisfied (use --upgrade to upgrade): Pyphen>=0.8 in /usr/local/lib/python2.7/dist-pack
ages (from WeasyPrint)
Requirement already satisfied (use --upgrade to upgrade): cffi>=0.6 in /usr/local/lib/python2.7/dist-packag
es (from WeasyPrint)
Requirement already satisfied (use --upgrade to upgrade): cairocffi>=0.5 in /usr/local/lib/python2.7/dist-p
ackages (from WeasyPrint)
Requirement already satisfied (use --upgrade to upgrade): tinycss==0.3 in /usr/local/lib/python2.7/dist-pac
kages (from WeasyPrint)
Requirement already satisfied (use --upgrade to upgrade): html5lib>=0.999 in /usr/local/lib/python2.7/dist-
packages (from WeasyPrint)
Requirement already satisfied (use --upgrade to upgrade): pycparser in /usr/local/lib/python2.7/dist-packag
es (from cffi>=0.6->WeasyPrint)
Requirement already satisfied (use --upgrade to upgrade): six in /usr/local/lib/python2.7/dist-packages (fr
om html5lib>=0.999->WeasyPrint)

[edit by admin: formatting]

There is no render_pdf in weasyprint -- why do you think there is one? Are you confusing it with flask-weasyprint?

Hey Dutchmo I think that might be the answer. When developing locally, we were using flask-weasyprint in order to send a template to download as a PDF. When looking to launch our app on pythonanywhere.com, Originally the flask-weasyprint was not working, so I changed it to just "weasyprint" which allowed me to import it but then threw the error on the render_pdf.

Am I just using the weasyprint module wrong? Here is my code for the print route

#PRINT YOUR BN!
@app.route('/pdf_template', methods=['POST', 'GET'])
def pdf_templates():

    owner = User.query.filter_by(email=session['email']).first()
    tasks = Task.query.filter_by(completed=False, owner=owner).all()


    html = render_template('pdf_template.html',
                                owner = owner,
                                tasks=tasks
                                )

    return render_pdf(HTML(string=html))

Is there a similar function to "render_pdf" already built into the weasyprint module?

Thanks in advance!

[edit by admin: formatting]

If you're using flask-weasyprint locally, it's probably best to use it on PythonAnywhere. It looks like your website is using Python 3.6, so you'll need to install it for that Python version by running this from a Bash console:

pip3.6 install --user Flask-WeasyPrint

Thanks giles, I had not installed the Flask-WeasyPrint. After getting that module installed, the render_pdf worked as designed locally.

:)