简体   繁体   English

如何在python-flask中添加自定义字体?

[英]How to add custom font in python-flask?

I have tried using @fontface css style, but the font doesn't get rendered. 我尝试过使用@fontface css样式,但字体不会被渲染。 Is there another way of doing this using python/flask?? 有没有其他方法使用python / flask?

<!DOCTYPE html>
<html>
<style type="text/css">
@font-face {
font-family: trial;
src: url_for('CENTRALESANSCND-BOLD.otf') ;
font-weight:bold; }

</style>
<body>

<p style="font-family:trial; font-weight: bold"> Hello </p>

</body>
</html>

The above is my HTML template. 以上是我的HTML模板。 Unfortunately, when I render it using Flask, it doesn't reflect the font. 不幸的是,当我使用Flask渲染它时,它不会反映字体。 The following is my .py code: 以下是我的.py代码:

from flask import Flask, render_template

app=Flask(__name__)
app.secret_key='1234'

@app.route('/', methods=['post', 'get'])
def output():
    c=2+3
    print c

    return render_template('f.html', c=c)

if __name__=='__main__':
    app.run(port=9876)

Thanks a lot for all your help.. A combination of all the suggestions finally worked. 非常感谢您的帮助..所有建议的组合终于奏效了。

Posting the solution here: 在这里发布解决方案:

CSS file: CSS文件:

@font-face{
font-family: trial;
src: url('CENTRALESANSCND-BOLD.otf');
font-weight: bold;}

body {font-family: georgia; color:green}
h1 {font-family:trial; color: pink}

HTML file: HTML文件:

<!DOCTYPE html>
<html>

<link type="text/css" rel="stylesheet" href="{{url_for('static', filename='mystyle.css')}}"/> 
<body>

<p > Hello... </p>
<h1> welcome </h1>

</body>
</html>

url_for takes a function name, and you need to wrap it in double curly brackets... try: url_for接受一个函数名称,你需要用双花括号括起来...尝试:

<style type="text/css">
    @font-face {
    font-family: trial;
    src: {{ url_for('static', filename='CENTRALESANSCND-BOLD.otf') }};
    font-weight:bold; }
</style>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM