Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add custom font in python-flask?

Tags:

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)
like image 498
Vidya Ravi Avatar asked Sep 15 '15 09:09

Vidya Ravi


1 Answers

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>
like image 93
Vidya Ravi Avatar answered Sep 28 '22 23:09

Vidya Ravi