该XML规范列出一串Unicode字符要么是非法或"望而却步".给定一个字符串,如何从中删除所有非法字符?
我提出了以下正则表达式,但它有点满口.
illegal_xml_re = re.compile(u'[\x00-\x08\x0b-\x1f\x7f-\x84\x86-\x9f\ud800-\udfff\ufdd0-\ufddf\ufffe-\uffff]')
clean = illegal_xml_re.sub('', dirty)
Run Code Online (Sandbox Code Playgroud)
(Python 2.5不知道0xFFFF以上的Unicode字符,所以不需要过滤那些.)
我想动态显示我的CPU使用情况.我不想重新加载页面以查看新值.我知道如何在Python中获得CPU使用率.现在我用值渲染一个模板.如何使用Flask中的值不断更新页面?
@app.route('/show_cpu')
def show_cpu():
cpu = getCpuLoad()
return render_template('show_cpu.html', cpu=cpu)
Run Code Online (Sandbox Code Playgroud) http.Handle("/", http.FileServer(http.Dir("static")))
Run Code Online (Sandbox Code Playgroud)
html
在静态目录中提供文件.
Go中是否有任何方法可以指定html
要提供的文件?
喜欢render_template
的东西Flask
我想做的事情如下:
http.Handle("/hello", http.FileServer(http.Dir("static/hello.html")))
Run Code Online (Sandbox Code Playgroud) 给出一个清单
a = [0,1,2,3,4,5,6,7,8,9]
Run Code Online (Sandbox Code Playgroud)
我怎样才能得到
b = [0,9,1,8,2,7,3,6,4,5]
Run Code Online (Sandbox Code Playgroud)
也就是说,产生一个新的列表,其中每个连续的元素交替地从原始列表的两边取出?
我正在关注docker教程,我正在使用以下部分来构建应用程序:
docker build -t friendlyhello .
Run Code Online (Sandbox Code Playgroud)
它达到第4步,暂停后我收到此错误:
Step 4/7 : RUN pip install -r requirements.txt
---> Running in 7f4635a7510a
Collecting Flask (from -r requirements.txt (line 1))
Retrying (Retry(total=4, connect=None, read=None, redirect=None)) after
connection broken by
'NewConnectionError('<pip._vendor.requests.packages.urllib3.connection.VerifiedHTTPSConnection
object at 0x7fe3984d9b10>: Failed to establish a new connection:
[Errno -3] Temporary failure in name resolution',)': /simple/flask/
Run Code Online (Sandbox Code Playgroud)
我不太清楚这个错误意味着什么以及如何解决它.
谢谢你的帮助!
我正在尝试将Flask-SQLAlchemy模型分成单独的文件.当我试着奔跑时,db.create_all()
我得到了No application found. Either work inside a view function or push an application context.
shared/db.py
:
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
Run Code Online (Sandbox Code Playgroud)
app.py
:
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from shared.db import db
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'My connection string'
db.init_app(app)
Run Code Online (Sandbox Code Playgroud)
user.py
:
from shared.db import db
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
email_address = db.Column(db.String(300), unique=True, nullable=False)
password = db.Column(db.Text, nullable=False)
Run Code Online (Sandbox Code Playgroud) 我有多个昂贵的函数返回结果.如果所有检查都成功,我想返回所有检查结果的元组.但是,如果一次检查失败,我不想调用后面的检查,如短路行为and
.我可以嵌套if
语句,但是如果有很多检查,这将失控.如何and
在保存结果供以后使用的同时获得短路行为?
def check_a():
# do something and return the result,
# for simplicity, just make it "A"
return "A"
def check_b():
# do something and return the result,
# for simplicity, just make it "B"
return "B"
...
Run Code Online (Sandbox Code Playgroud)
这不会短路:
a = check_a()
b = check_b()
c = check_c()
if a and b and c:
return a, b, c
Run Code Online (Sandbox Code Playgroud)
如果有很多检查,这很麻烦:
if a:
b = check_b()
if b:
c = check_c()
if c:
return a, b, …
Run Code Online (Sandbox Code Playgroud) 我有一个视图调用函数来获取响应.但是,它给出了错误View function did not return a response
.我该如何解决?
from flask import Flask
app = Flask(__name__)
def hello_world():
return 'test'
@app.route('/hello', methods=['GET', 'POST'])
def hello():
hello_world()
if __name__ == '__main__':
app.run(debug=True)
Run Code Online (Sandbox Code Playgroud)
当我尝试通过添加静态值而不是调用函数来测试它时,它可以工作.
@app.route('/hello', methods=['GET', 'POST'])
def hello():
return "test"
Run Code Online (Sandbox Code Playgroud) 我的User
模型与模型有关系Address
.我已经指定关系应该级联删除操作.但是,当我查询并删除用户时,我收到一个错误,即仍然引用了地址行.如何删除用户和地址?
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
addresses = db.relationship('Address', cascade='all,delete', backref='user')
class Address(db.Model):
id = db.Column(db.Integer, primary_key=True)
user_id = db.Column(db.Integer, db.ForeignKey(User.id))
Run Code Online (Sandbox Code Playgroud)
db.session.query(User).filter(User.my_id==1).delete()
Run Code Online (Sandbox Code Playgroud)
IntegrityError: (IntegrityError) update or delete on table "user" violates foreign key constraint "addresses_user_id_fkey" on table "address"
DETAIL: Key (my_id)=(1) is still referenced from table "address".
'DELETE FROM "user" WHERE "user".id = %(id_1)s' {'id_1': 1}
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Python 3.6运行基本的Flask应用程序.但是,我得到了一个ImportError: cannot import name 'ForkingMixIn'
.使用Python 2.7或3.5运行时,我不会收到此错误.如何使用Python 3.6运行Flask?
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello, World!"
Run Code Online (Sandbox Code Playgroud)
Traceback (most recent call last):
File "C:\Python36\lib\site-packages\werkzeug\serving.py", line 65, in <module>
from SocketServer import ThreadingMixIn, ForkingMixIn
ImportError: No module named 'SocketServer'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File ".\fsk.py", line 9, in <module>
app.run()
File "C:\Python36\lib\site-packages\flask\app.py", line 828, in run
from werkzeug.serving import run_simple
File "C:\Python36\lib\site-packages\werkzeug\serving.py", line 68, in …
Run Code Online (Sandbox Code Playgroud) python ×9
flask ×5
algorithm ×1
docker ×1
go ×1
http ×1
iteration ×1
javascript ×1
jinja2 ×1
list ×1
pip ×1
python-3.6 ×1
sqlalchemy ×1
ubuntu ×1
ubuntu-16.04 ×1
unicode ×1
werkzeug ×1
xml ×1