I'm using the following function to execute a simple HTML view:
import cherrypy
class index(object):
#cherrypy.expose
def example(self):
var = "goodbye"
index = open("index.html").read()
return index
Our index.html file is:
<body>
<h1>Hello, {var}!</h1>
</body>
How can I pass the {var} variable to the view from my controller?
I'm using CherryPy microframework to run the HTTP server and I'm NOT using any template engine.
change your html file and format it.
index.html
<body>
<h1>Hello, {first_header:}!</h1>
<p>{p2:}, {p1:}!</p>
</body>
The code
index = open("index.html").read().format(first_header='goodbye',
p1='World',
p2='Hello')
The Output
<body>
<h1>Hello, goodbye!</h1>
<p>Hello, World!</p>
</body>
Below code is working fine. Change the HTML and Python code accordingly
index.html
<body>
<h1>Hello, {p.first_header}</h1>
</body>
Python code
class Main:
first_header = 'World!'
# Read the HTML file
HTML_File=open('index.html','r')
s = HTML_File.read().format(p=Main())
print(s)
The Output
<body>
<h1>Hello, World!</h1>
</body>
CherryPy does not provide any HTML template but its architecture makes it easy to integrate one. Popular ones are Mako or Jinja2.
Source: http://docs.cherrypy.org/en/latest/advanced.html#html-templating-support
Related
this is my rule.html
<!DOCTYPE html>
<html>
<head>
<title>rules</title>
</head>
<body>
<H1>Have fun and enjoy {{name}}</H1>
<p>So there are some rules that will help you to play this game.</p>
</body>
</html>
this one is home page html code. Please help, I saw tutorials but not getting anything.
<!DOCTYPE html>
<html>
<head>
<title> Home</title>
</head>
<body>
<h1>Welcome to guess the game!!</h1>
<form method="post" action="/rule">
<h3>So, what is your name":</h3>
<input type="text" name="user">
<input type="submit" value="submit" >
</form>
</body>
this one is my python code.
from flask import Flask,render_template, request, redirect, url_for
app= Flask(__name__)
#app.route('/')
def home():
return render_template('home.html')
#app.route('/rule', methods=["POST","GET"])
def rule():
if request.method=="POST":
user=request.form["user"]
print(user)
return redirect(url_for("user",usr=user))
else:
return render_template("rule.html")
#app.route("/<usr>")
def user(usr):
return render_template("rule.html",name=usr)
if __name__ == '__main__':
app.run(debug=True)
sorry for so many codes. but i need help.
and my os is window 8.1, python -> 3.7.1, flask->1.1.1,werkzeug->1.0.1
error page
When I look at your error page, your browser seems to try to access D:/rule - this should not be served by the file system, but by Flask.
You need to access your app e.g. as localhost:5000/home.html and then it should work.
For me it looks like you directly access the html file in your browser from the file system.
First, you need to run your app, with something like python main.py, where main.py is the file name of your app.
Then enter the URL, which you see in the console plus append the home.html - it should work then.
hello I just started to program for the web with python without use of any web framework.
for testing reasons I created a simple html form with the post method to pass a simple text data to the WSGI app that I wrote.
this is the code for the HTML form:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>test</title>
</head>
<body>
<form action="/tester" method="post">
<input type="text" name="user">
<input type="submit" value="go">
</form>
</body>
</html>
and this is the code for the simple WSGI app:
def test_app(environ, start_response):
var1 = str(environ["REQUEST_METHOD"])
start_response('200 OK', [('Content-Type', 'text/plain')])
return[var1]
but no matter what I do it keeps showing me GET!(as the purpose of the code is tho return the method).
I don't know what I need to do?
I have a basic Flask app with the following structure :
from flask import Flask
from flask import render_template
app = Flask(__name__,template_folder='E:\Programming\Python Projects\Flask')
#app.route('/')
def index():
return render_template('hello.html')
#app.route('/route/')
def route1():
return render_template('route1.html')
app.run(debug = True,port = 8080,host = '0.0.0.0')
hello.html :
<!DOCTYPE html>
<html>
<head>
<title>Rendered!!</title>
</head>
<body>
<h1>
The template has been rendered!!!<br>
Route No. 1
</h1>
</body>
</html>
route1.html :
<!DOCTYPE html>
<html>
<head>
<title>Route No. 1</title>
</head>
<body>
<h2>
This is the first route!!!<br>
Hello World!!!
</h2>
<iframe src="https://www.youtube.com/embed/YQHsXMglC9A" width="853" height="480" frameborder="0" allowfullscreen></iframe>
</body>
</html>
When I open localhost:8080 it works fine.
But when I click on the link, it says :
The address wasn’t understood
Firefox doesn’t know how to open this address, because one of the following protocols (localhost) isn’t associated with any program or is not allowed in this context.
It works fine when I type the address localhost:8080/route manually in the address bar.
Also, it works fine when opened in a new tab.
I need help!!!
Thank You !!!
You should use from flask import render_template, url_for
and in the template:
<h1>
The template has been rendered!!!<br>
Route No. 1
</h1>
Just let Flask and Jinja2 make the URL's for you...
*It seems that you forgot the trailing slash at the link.
Should be localhost:8080/route/
But its far better to use url_for as it avoids this type of problem
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
I am handling with a Wikipedia-like project. I can convert the text file to html code using the markdown. My problem is, I want to render this html code in a html file. Here is my code,
class articles:
def GET(self):
form_name=web.input()
article_name=form_name.page
article_file_path=os.path.join('articles',article_name)
fp = open(article_file_path,'rU')
text = fp.read()
body = markdown2.markdown(text)
return render.article_files(article_name, body)
I'm passing article_name and body(html code) to article_files.html. The body looks like,
<h1>Hai</h1>
<p>Welcome<em>Ajay</em></p>
The problem is, the body displays as it is. That is the html code is printed in the screen with all tags. I want to render this html code (body) like,
Hai
Welcome Ajay
My HTML file is:
$def with(title,content)
<html>
<head>
<title>$title</title>
</head>
<body>
<form name="form" method="GET">
$content
</form>
</body>
</html>
HTML escaping is on by default in web.py templates. To turn it off, prepend the variable name with a colon:
<form name="form" method="GET">
$:content
</form>
Make sure there is no way for a potentially malicious user to feed arbitrary HTML into your unescaped templates.
You need to specify the mime type of the date you are sending to the browser, otherwise it doesn't know how to display it.
You can do this by adding the following line to your function:
web.header('Content-Type', 'text/html')