在python脚本中生成包含图像和文本的html文档(如果可能,不使用服务器)
如何在python中使用模板和css生成包含图像和文本的HTML 关于stackoverflow,几乎没有类似的问题(例如:,),但它们提供的解决方案(对我来说)似乎有些过分,比如需要服务器(例如genshi) 使用在python脚本中生成包含图像和文本的html文档(如果可能,不使用服务器),python,html,css,django,Python,Html,Css,Django,如何在python中使用模板和css生成包含图像和文本的HTML 关于stackoverflow,几乎没有类似的问题(例如:,),但它们提供的解决方案(对我来说)似乎有些过分,比如需要服务器(例如genshi) 使用django的简单代码如下: from django.template import Template, Context from django.conf import settings settings.configure() # We have to do this to use
django
的简单代码如下:
from django.template import Template, Context
from django.conf import settings
settings.configure() # We have to do this to use django templates standalone - see
# https://stackoverflow.com/questions/98135/how-do-i-use-django-templates-without-the-rest-of-django
# Our template. Could just as easily be stored in a separate file
template = """
<html>
<head>
<title>Template {{ title }}</title>
</head>
<body>
Body with {{ mystring }}.
</body>
</html>
"""
t = Template(template)
c = Context({"title": "title from code",
"mystring":"string from code"})
print t.render(c)
从django.template导入模板,上下文
从django.conf导入设置
settings.configure()#我们必须这样做才能单独使用django模板-请参阅
# https://stackoverflow.com/questions/98135/how-do-i-use-django-templates-without-the-rest-of-django
#我们的模板。可以很容易地存储在单独的文件中
模板=“”“
模板{{title}
具有{mystring}的主体。
"""
t=模板(模板)
c=上下文({“标题”:“来自代码的标题”,
“mystring”:“代码中的字符串”})
打印t.render(c)
(从这里开始:)
此代码产生错误,可能是因为我需要设置后端:
Traceback (most recent call last):
File "<input>", line 17, in <module>
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/template/base.py", line 184, in __init__
engine = Engine.get_default()
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/template/engine.py", line 81, in get_default
"No DjangoTemplates backend is configured.")
django.core.exceptions.ImproperlyConfigured: No DjangoTemplates backend is configured.
回溯(最近一次呼叫最后一次):
文件“”,第17行,在
文件“/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site packages/django/template/base.py”,第184行,在__
engine=engine.get_default()
文件“/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site packages/django/template/engine.py”,第81行,默认设置为get_
“未配置任何DjangoTemplates后端。”)
django.core.exceptions.ImpropertlyConfigured:未配置DjangoTemplates后端。
有没有一种简单的方法可以拥有template.html和style.css以及一组图像,并使用python脚本中的数据替换template.html中的占位符,而无需设置服务器?我使用的代码如下:
self.content = '''
<html>
<head>
''' + base_pyhtml.getAdmin ('documentation') + '''
<style>
''' + base_pycss.getAll () + '''
''' + documentation_pycss.getAll () + '''
</style>
</head>
<body>
''' + base_pyhtml.getFixed () + '''
<div class="moving">
<div class="documentation">
对我来说,它在实践中效果很好。这完全是用这种琐碎的技术完成的。我喜欢的是,我可以以这种方式使用Python的全部功能,而不是受到限制,甚至学习特殊的模板语法
通过使用.pyhtml和.pycss扩展并配置我的编辑器,我可以在这类代码上突出显示HTML和CSS语法,如下所示:
有很多python模板引擎-首先,您可能想看看这里:
就我而言,我会使用jinja2而不是YMMV。谢谢你的回答,他们帮我想出了一些适合我的东西 在下面的脚本中,我使用
jinja2
呈现一个html文件('index.html')
html模板是:
<!DOCTYPE html>
<html lang="en">
<head>
<!-- <link rel="stylesheet" type="text/css" href="styles.css"> -->
<style type="text/css">
#share-buttons img {
width: 32px;
padding: 5px;
border: 0;
box-shadow: 0;
display: inline;
}
body {
padding-left: 5px;
padding-right: 5px;
color: black;
max-width: 450px;
text-align: center;
}
</style>
<title>Your file 2016</title>
</head>
<body>
<h1>This has {{ no_users }} users:</h1>
<ul id="users">
{% for user in users %}
<li>{{user}}</li>
{% endfor %}
</ul>
{# STAGE 1 #}
{% if 1 in do_stages%}
<h1>Something worth showing</h1>
<p>
<img src="image.png" alt="caption" style="width:200px;height:200px;">
</p>
{% endif %}
{# STAGE 2 #}
{% if 2 in do_stages%}
<h1>Other header</h1>
<p>
{% for u in range(0,no_users) %}
<p>{{users[u]}} sent {{message_counts[u]}} messages.</p>
{% endfor %}
</p>
<p>
{% endif %}
<div id="share-buttons">
<!-- Twitter -->
<a href="https://twitter.com/share?url=https://stefanocosentino.com" target="_blank">
<img src="twitter_icon.png" alt="Twitter" />
</a>
</body>
</html>
#共享按钮{
宽度:32px;
填充物:5px;
边界:0;
盒影:0;
显示:内联;
}
身体{
左侧填充:5px;
右侧填充:5px;
颜色:黑色;
最大宽度:450像素;
文本对齐:居中;
}
你的档案2016
这有{{no_users}}}个用户:
{users%%中的用户为%s}
- {{user}}
{%endfor%}
{第一阶段}
{如果在do_阶段%1中为%1}
值得展示的东西
{%endif%}
{第二阶段}
{%if 2在do_阶段%}
其他标题
{范围(0,无用户)%%内的u为%
{{users[u]}发送了{{message_counts[u]}}条消息
{%endfor%}
{%endif%}
html文件具有在
中定义的
,并从本地文件夹“template”加载。错误只是告诉您需要将一些模板设置传递到设置中。configure()
。但是为什么不使用一个独立的模板系统呢?仅供参考:genshi不“需要服务器”。@Brunodesshuilliers,你能解释一下genshi不需要服务器的原因吗?我可能对这方面有点困惑,你也可以帮我澄清我的问题。在本教程()中,似乎通过调用“$PYTHONPATH=.python geddit/controller.py geddit.db”启动了服务器,以便在浏览器上显示结果。谢谢。@DanielRoseman-我不确定应该传递哪些模板设置?configure(DEBUG=True)会产生相同的错误。将研究Jinja2,但这不应该已经在django中可行了吗?
from jinja2 import Template, Environment, FileSystemLoader
# Render html file
env = Environment(loader=FileSystemLoader('templates'))
template = env.get_template('index.html')
output_from_parsed_template = template.render(no_users=len(users), do_stages=do_stages, users=users, message_counts=message_counts)
# to save the results
with open("OutputAnalysis.html", "w") as fh:
fh.write(output_from_parsed_template)
<!DOCTYPE html>
<html lang="en">
<head>
<!-- <link rel="stylesheet" type="text/css" href="styles.css"> -->
<style type="text/css">
#share-buttons img {
width: 32px;
padding: 5px;
border: 0;
box-shadow: 0;
display: inline;
}
body {
padding-left: 5px;
padding-right: 5px;
color: black;
max-width: 450px;
text-align: center;
}
</style>
<title>Your file 2016</title>
</head>
<body>
<h1>This has {{ no_users }} users:</h1>
<ul id="users">
{% for user in users %}
<li>{{user}}</li>
{% endfor %}
</ul>
{# STAGE 1 #}
{% if 1 in do_stages%}
<h1>Something worth showing</h1>
<p>
<img src="image.png" alt="caption" style="width:200px;height:200px;">
</p>
{% endif %}
{# STAGE 2 #}
{% if 2 in do_stages%}
<h1>Other header</h1>
<p>
{% for u in range(0,no_users) %}
<p>{{users[u]}} sent {{message_counts[u]}} messages.</p>
{% endfor %}
</p>
<p>
{% endif %}
<div id="share-buttons">
<!-- Twitter -->
<a href="https://twitter.com/share?url=https://stefanocosentino.com" target="_blank">
<img src="twitter_icon.png" alt="Twitter" />
</a>
</body>
</html>