Flask

How to add custom font in python-flask?

别说谁变了你拦得住时间么 提交于 2021-01-02 06:21:45
问题 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

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

Systemd launches duplicate python processes

∥☆過路亽.° 提交于 2021-01-02 03:03:27
问题 I am using systemd to start a python flask app on raspberry pi zero(Raspbian buster). Every time I start a service, it launches two python processes instead of one. Why does this happen? The first process is the parent of the second process. Here is my service definition in /etc/systemd/system/website.service: [Unit] Description=Website After=network.target [Service] User=root WorkingDirectory=/home/pi/dev ExecStart=python /home/pi/dev/app.py Restart=always [Install] WantedBy=multi-user

Flask基础(一)---模板渲染中的过滤器定义

只愿长相守 提交于 2021-01-01 06:47:07
Flask中模板渲染中过滤器的定义: 过滤器顾名思义:就是通过设置条件,筛选出我们需要的数据,在Flask项目中,我们在定义模板时,往往需要对显示的数据进行筛选,这就使用到了我们的过滤器. 使用方法: 1.首先在程序中,通过函数编写过滤条件 2.再声明这属于过滤器 3.然后将过滤器应用对象名传入模板 4.在模板中,通过{{ 变量 | 过滤器名 }} 5.过滤器也可以使用多次{{ 变量 | 过滤器 | 过滤器 }} from flask import Flask,render_template app = Flask(__name__) @app.route("/") def index(): return render_template("temp-demo2.html") # 方式一 # 自定义过滤器函数,命名不能与内置过滤器重名,不然会覆盖 # 自定义列表切片过滤器 def li_setup2(li): # 按照步长为2进行模板切片 return li[::2] # 通过模板过滤器注册两个参数(自定义过滤器函数名, 模板过滤器变量名) app.add_template_filter(li_setup2, "li2") # 方式二 # 通过装饰器传入模板过滤器名称,内部实现过滤器功能再返回, # 模板直接调用过滤器名称即可 # 如下自定义一个列表反转过滤器 @app

How Handle a button click on python/Flask

老子叫甜甜 提交于 2021-01-01 06:34:58
问题 I'm building an small application and want to handle a button click using Flask. What do i need to add to get it working> If you have the following in the template: <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> </head> <body> <div class="container"> <h2>Button Pressed: {{ButtonPressed}} </h2> <input type="submit" value="Click Me" > </div> </body> And the following in .py Button

Azure deployment not installing Python packages listed in requirements.txt

一曲冷凌霜 提交于 2021-01-01 04:55:09
问题 This is my first experience deploying a Flask web app to Azure. I followed this tutorial. The default demo app they have works fine for me. Afterwards, I pushed my Flask app via git. The log shows deployment was successful. However, when I browse the hosted app via link provided in "Application Properties", I get a 500 error as follows: The page cannot be displayed because an internal server error has occurred. Most likely causes: IIS received the request; however, an internal error occurred

python web开发-flask中读取txt文件内容

佐手、 提交于 2020-12-31 09:35:50
某些情况下,需要读取flask网站要目录下的txt文件。但是直接在flask网站的目录下创建一个文件是无法访问的。从网站找了一些资料,最终发现通过写一个方法返回txt内容比较简单方便,不过此方法适用于简单的文件读取以及读取量比较小的时候。详细代码如下: @app.route( '/<path>' ) def today(path): base_dir = os.path.dirname(__file__) resp = make_response(open(os.path.join(base_dir, path)).read()) resp.headers[ "Content-type" ]= "text/plan;charset=UTF-8" return resp 测试方法,在要目录创建一个readm.txt文件。 运行程序,访问/readme.txt,返回结果如下: 延伸知识点: 因为在之前的代码中做过一个简单的url实例,url的定义和本文的定义一样,只是参数名不一样,如下: 这时候就遇到一个问题,当我把今天的url定义放到最后面的时候,其实访问的是/<name>这个url,会一直返回hello xxx, 然后把本文例子的代码放到/<name>这个接口的上面,再次运行,得到正确的结果。 更多文章,关注微信公众号”挨踢学霸” 来源: oschina 链接: https:/

Flask-Swagger-UI does not recognize path to swagger.json

会有一股神秘感。 提交于 2020-12-31 07:39:03
问题 I'm building an API, using Flask and flask-restful and flask-swagger-ui. I have now modified the project structure and now I can no longer access the project's swagger.json file. Based on the package documentation flask-swagger-ui, you would only need to change the parameter API_URL to the correct path. But even when entering relative path or full path, I can no longer access the file. My Code : from flask import Flask, jsonify from flask_migrate import Migrate from flask_restful import Api

Flask-Swagger-UI does not recognize path to swagger.json

浪尽此生 提交于 2020-12-31 07:38:08
问题 I'm building an API, using Flask and flask-restful and flask-swagger-ui. I have now modified the project structure and now I can no longer access the project's swagger.json file. Based on the package documentation flask-swagger-ui, you would only need to change the parameter API_URL to the correct path. But even when entering relative path or full path, I can no longer access the file. My Code : from flask import Flask, jsonify from flask_migrate import Migrate from flask_restful import Api

Using proper file structure with SQLAlchemy and how to add data to db

感情迁移 提交于 2020-12-31 06:51:06
问题 I am trying to build a simple blogging platform to learn Python and Flask. I am using SQLAlchemy to connect to a Postgres db hosted on Heroku and flask_s3 to serve static files from an AWS bucket. Im mostly following along from this: https://gist.github.com/mayukh18/2223bc8fc152631205abd7cbf1efdd41/ All was going well, it is correctly hosted on Heroku and connceted to AWS S3 bucket, ready to go. I am stuck however on how to add blog posts to the database through some kind of form or route