I am new to chameleon templates. i paste code snippet ..
runtemp.py
import os
path = os.path.dirname(__file__)
from chameleon import PageTemplateLoader
templates = PageTemplateLoader(os.path.join(path, "templates"))
template = templates['mytemp.pt']
template(name='John')
print str(template.read())
mytem.pt
<testtag>
<innertesttag>${name}</innertesttag>
</testtag>
But the output i got is
<testtag>
<innertesttag>${name}</innertesttag>
</testtag>
I was expectinng John in output instead od $(name)
What is going wrong ? how to render template?
template.read() just reads the contents of the template; you discarded the actual rendering result. template(name='John') returns the rendering.
Do this instead:
print template(name='John')
Related
I am trying to create a very simple one-page Flask application for a python script that I have. The script requires multiple user inputs in a for-loop with the number of loops being user input as well.
Here is the code in my script to make it more clear:
def shared_books():
import requests as re
from bs4 import BeautifulSoup
import time
num_lists = int(input('Enter the number of lists you would like to search:'))
urls = []
page_counts = []
for i in range(num_lists):
urls.append(input(f'Enter the url for list {i + 1}:'))
page_counts.append(int(input(f'Enter the number of pages for list {i + 1}:')))
I want a simple HTML that will ask the user for the number of lists, then the URL and page count for each list as is shown in my function. Then it will run the entire function.
The HTML code I have right now is super simple and I don't want much else outside of the input parts:
<html>
<head>
<title>Goodreads-App</title>
</head>
<body>
<h1>Welcome to my app!</h1>
<<p>This app will allow you to see books that are
shared between multiple lists on goodreads</p>
</body>
</html>
Please let me know how I can set up this application!
Firstly, I suggest you take a look at the Flask docs. You are doing it right in terms of having a view function, but the input() python keyword doesn't work like that in Flask. Instead, you should render an html template which you can then put your form input field into. Here is an example:
from flask import Flask, render_template
#flask initialising stuff, read docs for info
#app.route("/home")
def home():
return render_template("home.html")
Flask runs on your computer's local server "localhost", which is not publicly accessible. It conventionally runs on port 5000, which gives the name "localhost:5000".
When someone visits "localhost:5000/home", flask will look for a file called "home.html" in a pre-designated templates folder – the default is a directory called "templates" which you should put your html files into.
So if this is your "home.html" file:
<html>
<head>
<title>Goodreads-App</title>
</head>
<body>
<h1>Welcome to my app!</h1>
<p>This app will allow you to see books that are
shared between multiple lists on goodreads</p>
</body>
</html>
When you load the page associated with a specific function, it will return a template which is rendered as html. The above should look something like this:
And that is how to start.
Thank you for the answers! I haven't quite solved the previous issue but have approached it from a different angle which is working now! I will potentially post again if I don't solve it.
I am using flask forms to do what I was trying.
I want to design my own HTML template with tags like JSP or Jade and then pass data from python to it and let it generate full html page.
I don't want to construct document at python side like with DOM. Only data goes to page and page tempalte decides, how data lays out.
I don't want to serve resulting pages with HTTP, only generate HTML files.
Is it possible?
UPDATE
I found Jinja2, but I has strange boilerplate requirements. For example, they want me to create environment with
env = Environment(
loader=PackageLoader('yourapplication', 'templates'),
autoescape=select_autoescape(['html', 'xml'])
)
while saying that package yourapplication not found. If I remove loader parameter, it complains on line
template = env.get_template('mytemplate.html')
saying
no loader for this environment specified
Can I just read template from disk and populate it with variables, without extra things?
Just use the FileSystemLoader:
import os
import glob
from jinja2 import Environment, FileSystemLoader
# Create the jinja2 environment.
current_directory = os.path.dirname(os.path.abspath(__file__))
env = Environment(loader=FileSystemLoader(current_directory))
# Find all files with the j2 extension in the current directory
templates = glob.glob('*.j2')
def render_template(filename):
return env.get_template(filename).render(
foo='Hello',
bar='World'
)
for f in templates:
rendered_string = render_template(f)
print(rendered_string)
example.j2:
<html>
<head></head>
<body>
<p><i>{{ foo }}</i></p>
<p><b>{{ bar }}</b></p>
</body>
</html>
I have flask, jinja2 and python.
So, I'm trying to display text that is stored as markdown.
I do this
class Article(db.Entity):
...
def html(self):
return markdown(self.text) # from markdown import markdown
Next in my view I do this
html_text = article_.html()
return render_template('article.html', article=article_, comments=comments, user=user, text=html_text)
And in article.html I just have this line
{{text}}
So, with data stored in db as *im busy* I have <p><em>im busy</em></p> in my browser.
I tried to use .replace('<', '<').replace('>', '>') but it changes nothing.
Do you know safe filter?
{{text|safe}}
Passing HTML to template using Flask/Jinja2
I am trying to render html output that is generated by a python google maps library that involves JS code in it. I am passing the part that shows google map with the html_map variable, and as follows:
html = t.render(Context({'html_map':html_map}))
return HttpResponse(html)
However, instead of showing the map, the page shows js code(i.e., directly prints it). The image below shows this:
How can I solve this?
html = t.render(Context({'html_map':html_map}))
return HttpResponse(html)
use in template:
{{ htm_map|safe }}
I am new to mako, and have a question about the object model.
We are generating a template file via mako under Windows 7 through the render command, similar to
out.write(self.objectname.render(...))
within the file being rendered, I want to access the objectname similar to the following pseudo code ...
<%namespace name="mapping" module="objtool.mapping" />
<%!
import os
import time
%>\
======================================================
== this file is being rendered from $(object.name)
======================================================
Is this even possible?
Note: I can get it to generate output similar to the following
this file is being rendered from <mako.template.Template object at 0x02F48990>
but I want the actual object name
<%namespace name="mapping" module="objtool.mapping" />
<%!
import os
import time
%>\
======================================================
== this file is being rendered from ${os.path.basename(self.name)}
======================================================
renders
======================================================
== this file is being rendered from poco_custom.cs
======================================================