我正在使用app工厂模式来设置我的Flask应用程序.我的应用程序使用Flask-Babel扩展,也在工厂中设置.但是,我想在蓝图中访问扩展程序以便使用它,
工厂在__init__.py.
def create_app(object_name):
app = Flask(__name__)
app.config.from_object(object_name)
babel = Babel(app)
app.register_blueprint(main_blueprint)
app.register_blueprint(category_blueprint)
app.register_blueprint(item_blueprint)
db.init_app(app)
return app
Run Code Online (Sandbox Code Playgroud)
我想将以下内容添加到main.py:
@babel.localeselector
def get_locale():
if 'locale' in session:
return session['locale']
return request.accept_languages.best_match(LANGUAGES.keys())
@application.route('/locale/<locale>/', methods=['GET'])
def set_locale(locale):
session['locale'] = locale
redirect_to = request.args.get('redirect_to', '/')
return redirect(redirect_to) # Change this to previous url
Run Code Online (Sandbox Code Playgroud)
遗憾的是,main.py无法babel从应用程序工厂访问变量.我该如何解决这个问题?
好的,我有以下应用程序:
[python: **.py]
[jinja2: **/templates/**.html]
extensions=jinja2.ext.autoescape,jinja2.ext.with_
Run Code Online (Sandbox Code Playgroud)
babel = Babel()
def create_app(object_name):
app = Flask(__name__)
app.config.from_object(object_name)
app.register_blueprint(main_blueprint)
app.register_blueprint(category_blueprint)
app.register_blueprint(item_blueprint)
babel.init_app(app)
db.init_app(app)
return app
@babel.localeselector
def get_locale():
return 'de'
Run Code Online (Sandbox Code Playgroud)
main_blueprint = Blueprint(
'main',
__name__,
template_folder='../templates/main',
)
@main_blueprint.route('/debug')
def debug():
print get_locale()
return gettext('A simple string')
Run Code Online (Sandbox Code Playgroud)
我运行了以下命令:
这是我的项目结构的样子:
.
|-- README.md
|-- babel.cfg
|-- fabfile.py
|-- lucy
| |-- …Run Code Online (Sandbox Code Playgroud) 我有一个应用程序,刺猬(https://github.com/qfq8wf77qwf8/hedgehog-test),我正在heroku上部署它。这是文件结构:
\n\nhedgehog/\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 Procfile\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 README.md\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 hedgehog\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 __init__.py\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 __init__.pyc\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 app.db\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 simple_page\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 __init__.py\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 __init__.pyc\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 models.py\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 models.pyc\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 views.py\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 views.pyc\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 hedgehog.egg-info\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 PKG-INFO\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 SOURCES.txt\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 dependency_links.txt\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 top_level.txt\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 requirements.txt\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 setup.py\nRun Code Online (Sandbox Code Playgroud)\n\n关于我的设置的一些注意事项:
\n\n目前,我hedgehog通过本地安装应用程序pip install -e .\n这工作正常 - 我可以使用flask run和运行我的应用程序heroku local运行我的应用程序。我还可以在 heroku 上部署这个应用程序。
我唯一的问题是,当我执行 pip freeze …
我有以下代码:
class StatusTask(automata_celery.Task):
def on_success(self, retval, task_id, args, kwargs):
with app.app_context():
cloaker = Cloaker.query.get(args[0])
cloaker.status = RemoteStatus.LAUNCHED
db.session.commit()
def on_failure(self, exc, task_id, args, kwargs, einfo):
with app.app_context():
cloaker = Cloaker.query.get(args[0])
cloaker.status = RemoteStatus.ERROR
db.session.commit()
@celery.task(base=StatusTask)
def deploy_cloaker(cloaker_id):
"""To prevent launching while we are launching, we will
disable launching until the cloaker's status is LAUNCHED
"""
cloaker = Cloaker.query.get(cloaker_id)
if not cloaker.can_launch():
return
cloaker.status = RemoteStatus.LAUNCHING
db.session.commit()
host = cloaker.server.ssh_user + '@' + cloaker.server.ip
execute(fabric_deploy_cloaker, cloaker, hosts=host)
def fabric_deploy_cloaker(cloaker):
domain = …Run Code Online (Sandbox Code Playgroud) 我编写了自己的自定义客户端,通过我的wifi卡将原始http请求发送到我的烧瓶网络服务器.
这就是典型的请求:
Content-Length: 214
User-Agent: blah
Connection: close
Host: 1.2.3.4:5000
Content-Type: application/json
{"data":[{"scoutId":2,"message":"ph=5.65"},{"scoutId":4,"message":"ph=4.28"},{"scoutId":3,"message":"ph=4.28"},{"scoutId":2,"message":"ph=5.65"},{"scoutId":4,"message":"ph=4.28"},{"scoutId":3,"message":"ph=4.30"}]}
Run Code Online (Sandbox Code Playgroud)
有时,我的客户搞砸了并将错误的JSON请求发送到我的烧瓶服务器.通常,烧瓶只会显示:
1.2.3.5 - - [01/Sep/2014 22:13:03] "POST / HTTP/1.1" 400 -
Run Code Online (Sandbox Code Playgroud)
并没有提供有关请求的信息.
我想跟踪在我的环境中产生400的每个请求,并分析导致这些错误的原因.
我在哪里可以将自定义错误功能放在我的烧瓶服务器中?
我有以下文件:
from fabric.api import env, execute, run
env.hosts = ['1.2.3.4']
def taskA():
run('ls')
def main():
try:
execute(taskA)
except:
print "Exception Caught"
main()
Run Code Online (Sandbox Code Playgroud)
我运行时可以看到"Exception Caught"打印出来:
$ python test.py
[1.2.3.4] Executing task 'taskA'
[1.2.3.4] run: ls
Fatal error: Timed out trying to connect to 1.2.3.4 (tried 1 time)
Underlying exception:
timed out
Aborting.
Exception Caught
Run Code Online (Sandbox Code Playgroud)
但是,当我将其切换为:
def main():
try:
execute(taskA)
except Exception, e:
print "Exception Caught", e
main()
Run Code Online (Sandbox Code Playgroud)
我没有看到异常被捕获:
[1.2.3.4] run: ls
Fatal error: Timed out trying to connect to 1.2.3.4 (tried …Run Code Online (Sandbox Code Playgroud) 我有两个性质相似的类,但它们具有不同的功能签名。我正在考虑拥有两个接口还是拥有一个接口。让我举例说明:
public interface RaceCar {
CompletableFuture<Double> drive(final Wheel arg1, Tactic tactic);
}
public interface Bus {
CompletableFuture<Double> drive(final String someOtherKey, final Controller arg2);
}
Run Code Online (Sandbox Code Playgroud)
一个抽象类 AbstractCar 在两者之间共享逻辑:
public abstract AbstractCar {
// Add shared logic here...
}
Run Code Online (Sandbox Code Playgroud)
以及实施。
public class RaceCarImpl extends AbstractCar implements RaceCar {
...
}
public class BusImpl extends AbstractCar implements Bus {
...
}
Run Code Online (Sandbox Code Playgroud)
结果:2 个接口、1 个抽象、2 个实现 = 5 个文件。
public interface Vehicle {
CompletableFuture<Double> drive(final Wheel arg1, Tactic tactic);
CompletableFuture<Double> drive(final String …Run Code Online (Sandbox Code Playgroud) 我正在 Pinoccio 上运行一些代码,它是 Arduino 的一个分支。我在下面的代码中泄漏了内存,并且我已经包含了我的程序运行与时间的内存占用。
过去几个小时我一直在调试为什么我的内存正在慢慢泄漏,所以我决定试一试这个论坛。
#include <SPI.h>
#include <Wire.h>
#include <Scout.h>
#include <GS.h>
#include <bitlash.h>
#include <lwm.h>
#include <js0n.h>
#include <QueueArray.h>
#include <Oyoroi.h>
#include <aJSON.h>
#include <MemoryFree.h>
#define SERVER_LOCATION "1.2.3.4"
#define SERVER_PORT 5000
int INTERVAL_QUERY_SERVER = 10000;
struct OMessage {
int scoutId;
String message;
int messageKey;
};
QueueArray<OMessage*> queue;
GSTcpClient *clientPtr;
GSModule *gsPtr;
bool isOyoroiVerbose = 0;
void setup() {
Scout.setup("Custom", "Unknown", -1);
Serial.begin(115200);
Shell.isVerbose = false;
Shell.eval("rm *"); // Clean up previous installations
Shell.eval("hq.verbose(0)");
Shell.eval("events.verbose(0)");
Shell.eval("mesh.verbose(0)");
addBitlashFunction("oyoroi.verbose", (bitlash_function) …Run Code Online (Sandbox Code Playgroud) 我目前正在我的网站上进行一些调试,其中涉及调用 facebook API。
我已经安装了 dnsmasq 与我的 mac os X 一起使用,将所有请求重定向到 facebook.com 到 127.0.0.1
我有一个回显服务器,它将在我的笔记本电脑上的端口 80 上打印出所有原始的 http 请求标头。
现在我的问题来了。当我访问 facebook.com 时,我意识到 chrome 会自动将 http:// 转发到 https:// for facebook.com
我用谷歌搜索并找到了删除这个 HSTS 问题的方法。我chrome://net-internals#hsts去看看是这样的:
在“删除域”下输入“facebook.com”后,我仍然可以在下面的输入框中查询“facebook.com”。
我尝试清除 chrome 上的所有用户数据,关闭并重新打开 chrome,甚至使用隐身模式。
为什么 chrome 仍在将所有对 facebook.com 的请求重定向到 https?
如果chrome://net-internals#hsts不可靠,我该如何禁用它?
我有以下代码:
DSPOTTGuaranteedUserReachForecastModel model = new DSPOTTGuaranteedUserReachForecastModel(
mockCache,
demand,
mockTargetingDatabaseHelper,
programType,
region);
Run Code Online (Sandbox Code Playgroud)
正确的缩进应该是每条不完整的行缩进两个。理想情况下,我希望我的文本编辑器将我的代码格式化为这样:
DSPOTTGuaranteedUserReachForecastModel model = new DSPOTTGuaranteedUserReachForecastModel(
mockCache,
demand,
mockTargetingDatabaseHelper,
programType,
region);
Run Code Online (Sandbox Code Playgroud)
然而,当我尝试使用 VSCode 对其进行格式化时,所有新行都折叠成一长行。
DSPOTTGuaranteedUserReachForecastModel model = new DSPOTTGuaranteedUserReachForecastModel(mockCache, demand,
mockTargetingDatabaseHelper, programType, region);
Run Code Online (Sandbox Code Playgroud)
有谁知道保留行号的规则的名称是什么?
python ×7
flask ×3
java ×2
arduino ×1
babel ×1
c++ ×1
celery ×1
fabric ×1
flask-admin ×1
flask-babel ×1
format ×1
git ×1
heroku ×1
hsts ×1
memory-leaks ×1
pip ×1
python-babel ×1
ssl ×1