简体   繁体   中英

How to add custom font in python-flask?

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)

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>

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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