How to add custom font in python-flask?

生来就可爱ヽ(ⅴ<●) 提交于 2021-01-02 06:21:10

问题


I have tried using @fontface css style, but the font doesn't get rendered. Is there another way of doing this using 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. Unfortunately, when I render it using Flask, it doesn't reflect the font. The following is my .py code:

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)

回答1:


Thanks a lot for all your help.. A combination of all the suggestions finally worked.

Posting the solution here:

CSS file:

@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:

<!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>



回答2:


url_for takes a function name, and you need to wrap it in double curly brackets... try:

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


来源:https://stackoverflow.com/questions/32582644/how-to-add-custom-font-in-python-flask

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!