so I am very new to flask and am tinkering around with the CTypes module aswell - playing around with C & C++ files compiled in .so files to utilize in Python... I have a simple function imported into python using CTypes and then display the return value of the function (A random number to the power of two; x^2) using Flask into an html file along with a little sample introduction in case I were to stumble on this file a year from now - I would clearly know why I made this random sample. Now, that's all fine and dandy but, I heard on the street of the internets I can import multiple(all) my python variables into my HTML template using **locals(). I have seen others get this to work, but alas - I cannot... I'll whip up a Python function to replace the C++ file so you all don't have to mess with it... That works fine and is simply a part of this file and not inherently apart of the problem. I am naive enough to were I am overlooking something completely and the CTypes module is the root of this dilemma possibly.
from flask import Flask, render_template
# disabled the CTypes module for your convenience...
# from ctypes import *
def c_plus_plus_replacement(x):
return pow(x, 2)
# libpy = CDLL("./libpy.so")
# value = libpy.main(10)
value = c_plus_plus_replacement(5)
name = "Michael"
app = Flask(__name__)
@app.route("/")
def index():
# ---- The problem is at this return statement to render the HTML template...
# render_template("base.html", value=value, name=name) works fine, but I would like this statement to work...
return render_template("base.html", value=value)
if __name__ == '__main__':
app.run(debug=False)
Let me know if you can help! :)
As it appears in your code,
nameandvalueare defined globally, therefore are not local to the functionindex, so they wouldn't appear inlocals()when you calllocalsfrom within theindexfunction.This would work if you moved these inside the function...
This would make the names
valueandnameavailable in the template being rendered.