I am able to execute the python file through shell like so:
$ python jinja.py
[code]
from jinja2 import Environment, FileSystemLoader
DIR = '/Users/username/Sites'
env = Environment(loader=FileSystemLoader(DIR))
templateVars = {
"title" : "Test Example",
"description" : "Description"
}
template = env.get_template('index.html')
print template.render(templateVars)
[/code]
Here is the ouput via the shell:
[code]
<html>
<head>
<title>Test Example</title>
<meta name="description" content="Description">
</head>
<body>
test dictionary
</body>
</html>
[/code]
However, when I pull up index.html on the browser it doesn't render the variable, I am not sure the file jinja.py is even being executed.
Here is the sourcecode directly from my the browser window:
[code]
<html>
<head>
<title>{{ title }}</title>
<meta name="description" content="{{ description }}">
</head>
<body>
test dictionary
</body>
</html>
[/code]
Fyi, I am not using jinja2 in conjunction with any frameworks or other package dependencies.
Anyone able to help out.
Thanks
Mark
Your http://www.example.com/index.html should GET a script, which uses jinja to render the HTML.
You need a framework like webapp2 in Google App Engine to handle the GET.
I found this tutorial: https://www.youtube.com/watch?v=XyGW0ExGHDQ
Or use: https://developers.google.com/appengine/docs/python/gettingstartedpython27/introduction
Related
Sorry for my English.
I am want to study python library (eel). I am wrote the following code as in the example from the documentation:
The "index.html" file is located in the "Web" folder:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Hello</title>
<script type="text/javascript" src="/eel.js"></script>
<script type="text/javascript">
eel.python_func("Hello from Python");
</script>
</head>
<body>
Hello!!
</body>
</html>
The "main.py" file is located one level above the "index.html" file:
import eel
eel.init("web")
#eel.expose
def python_func(text):
print(text)
eel.start("index.html")
Running main.py displays a browser window that says "Hello !!" but python_func() dont started. Message "Hello from Python" dont displayed in terminal.
I am tried different options for solutions to problem:
reinstalled eel
reinstalled python
pasted the python_func () function code right after import.
But this solutions not helped.
I ask for help in solving my problem.
I am using Python 3.7.6 32 bit
I am using VS code in a Windows 10 environment to debug a python flask\jinja application.
Python code pass data to html page without issue and {{ formDict }} displays data correctly. Debugging shows there are data passed to javascript. However, running to this line
var dict = json.stringfy('{{ formDict|tojson|safe }}');
generates this error
toJson is not defined
Is this VS code issue that not load jinja or some other problem? Any recommendation how to resolve it?
Python code:
formDict = {"firstname": "Eric", "lastname": "Smith"}
return render_template("appointment.html",formDict=formDict)
html file - appintment.html
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
<script>
function createPointOption () {
var dict = json.stringfy('{{ formDict|tojson|safe }}');
......
}
</script>
</head>
<body onload="creatPointOption()">
....
<p id="test">{{formDict}}</p>
</body>
</html>
Are you sure your error is tojson is not defined. When I ran your code, I got json is not defined.
Try:
var dict = JSON.stringify('{{ formDict|tojson|safe }}');
Note capatilisation of JSON
I have a basic HTML file which looks like this:
<!DOCTYPE html>
<html>
<head>
<title>Home</title>
</head>
<body>
<h1>Welcome!</h1>
</body>
</html>
I am receiving the file in python and storing it as a string. I want to know, is there a way I can write this out to a web browser?
The file is on my computer, so my goal is not to save it as an html file and then execute it, but rather execute this from within python to the browser.
I know that with JavaScript I can use Document.write() to inject content to a webpage, but that is already being done in the browser. I want to achieve something similar.
You can use flask, a simple Python web framework, to serve the string:
Using flask (pip install flask):
import flask
app = flask.Flask(__name__)
s = """
<!DOCTYPE html>
<html>
<head>
<title>Home</title>
</head>
<body>
<h1>Welcome!</h1>
</body>
</html>
"""
#app.route('/')
def home():
return s
if __name__ == '__main__':
app.debug = True
app.run()
Now, can you navigate to 127:0.0.1:5000 or the equivalent IP and port specified when the app is run.
You could do the following:
html = """<!DOCTYPE html>
<html>
<head>
<title>Home</title>
</head>
<body>
<h1>Welcome!</h1>
</body>
</html>"""
with open('html_file.html', 'w') as f:
f.write(html)
import webbrowser, os
webbrowser.open('file://' + os.path.realpath('html_file.html'))
I have a simple "hello world" VueJS app I'm trying to get working:
<!doctype html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="https://unpkg.com/vue"></script>
</head>
<body>
<div id="app">
Message: {{ message }}
</div>
<script>
var vm = new Vue({
el: "#app",
data: {
message: "Hello, world"
}
});
</script>
</body>
</html>
When I load this file in the browser, off my local disk (ie: file:///home/user/vue-project/index.html), it loads and "Hello, world" is displayed.
However, if I try to take the same file and serve it through the python flask development server, or through gunicorn, {{message}} renders blank.
Does anyone know what might be causing that to happen?
flask renders its variables with jinja2 which uses {{ variable }} as its parsing delimiter
render("mytemplate.html",message="Hello") would replace all {{ message }} blocks with "Hello" before any javascript is handled ... since you dont define message it is simply an empty string... you will need to configure vue to use alternative delimiters (I use [[ message ]])
<!doctype html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="https://unpkg.com/vue"></script>
</head>
<body>
<div id="app">
Message: [[ message ]]
</div>
<script>
var vm = new Vue({
el: "#app",
delimiters : ['[[', ']]'],
data: {
message: "Hello, world"
}
});
</script>
</body>
</html>
I am using Jinja2 with python 3.3.1 and my templatecode is the following:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>{{ title }}</title>
<meta name="description" content="{{ description }}" />
</head>
<body>
<div id="content">
<p>Why, hello there!</p>
</div>
</body>
</html>
and my python.cgi file is the following :
from jinja2 import Template
print("Content-type: text/html\n\n")
templateLoader = jinja2.FileSystemLoader( searchpath="\\")
templateEnv = jinja2.Environment( loader=templateLoader )
TEMPLATE_FILE = "cgi-bin/example1.jinja"
template = templateEnv.get_template( TEMPLATE_FILE )
templateVars = { "title" : "Test Example",
"description" : "A simple inquiry of function." }
outputText = template.render( templateVars )
And all I am getting is a blank page with no html, the cgi-header is working meaning the browser is recognizing that its html but 'Why, hello there' is not being displayed. jinja2 is working too since in interpreter mode I created a simple template like :
t = Template("hello! {{title}}")
t.render(title="myname")
and it displayed hello! myname
Nothing wrong in the error_log either. Whats going on?
The Python interpreter auto-echoes the result of any expression as long as it is not returning None.
In a CGI script you need to explicitly write the result out:
outputText = template.render( templateVars )
print(outputText)
template.render() only produces the string result, it doesn't write this to stdout for you.