Forums

Running a standalone script with a virtualenv and html templates

Hi

I am trying to run a script on its own for testing purposes, and i'm facing some challenges.

  1. it needs yagmail, which has to be imported in a virtualenv. So currently to run the code I am going in the bash console and writing :

    workon my-venv && python /home/myname/mysite/send_yagmail.py

I am wondering if there is an easier way, like using directly the console of the code itself and not the bash?

2 . I can't manage to produce my email using templates, the code is giving "AttributeError: 'NoneType' object has no attribute 'app'"

My code :

import yagmail
from flask import render_template, Flask

yag=yagmail.SMTP("login","pwd")
receiver_email = "testaddress@gmail.com"
name1 = "Smith"
body = render_template("/templates/email.html", name = name1)
yag.send(to=receiver_email, contents=body)

It seems that it's not taking the 'render_template' function, and I don't understand why, as i've used it in my main 'flask_app'.

It's showing me an error with ctx.app.update_template_context(context), and the little i've seen here, the difference is that i'm not running my app here, just a script, so i'm not sure how it translates...

Thank you for your help!

Regarding running the script -- your way of doing it is fine, though it's worth noting that if you keep the Bash console open, you only need to workon the virtualenv once -- after that, you can just run python /home/myname/mysite/send_yagmail.py.

Regarding running Flask code in scripts that are separate from your website -- Flask needs to do various configuration things in order to have its helper functions fully operational. Try setting the FLASK_APP environment variable; for example, if the script that contains your app object is called flask_app.py, you would run this Bash command before running your script:

export FLASK_APP=flask_app

Thanks Giles. Just an update : i couldn't make it work. Had to rewrite the template in the script and move on for now.