0

How would I go about refreshing or re-rendering a page?

So I want to have a separate function like so:

changed_var = "bb"

def Check():
    changed_var = "xx"


app = Flask(__name__)

@app.route("/")
def server_1():
    render_template_string('index.html', changed_var="bb")

app.run()

so the changed_var is changing every second or so, how would I keep calling server_1, so that it re renders and changed the changed_var? I have tried calling server_1 from Check() but have ran into errors. Essentially, I want to call flask and tell it to "re render" from a "normal" function.

KingPey
  • 29
  • 6

1 Answers1

1

See if this gets you through, might be a simple typo you've given to the template engine which takes the template variable and always provides a literal "bb" value to it.

changed_var = "bb"

def Check():
    changed_var = "xx"


app = Flask(__name__)

@app.route("/")
def server_1():
    render_template_string('index.html', changed_var=changed_var)

app.run()

Essentially the variable and value go like this

render_template_string('index.html', templateVarName=pythonVarName )

but people don't often do that explicit name difference since if you're grepping your codebase you'd like to land on all the instances no matter their context. As a developer though you need to keep track of what context your variable is being utilized.

jxramos
  • 7,356
  • 6
  • 57
  • 105
  • the problem is this only works once... so okay it loads the right variable... but then changed_var changes again to "blah blah"... but how do i trigger server_1()? – KingPey Feb 02 '18 at 02:31
  • @KingPey Using `ajax` to update the value of `changed_var` – zeleven Feb 02 '18 at 02:34
  • Here's a generic approach to a poling refresh timer solution; doesn't really have anything specific to Flask however https://stackoverflow.com/questions/2787679/how-to-reload-page-every-5-second – jxramos Feb 02 '18 at 02:44