小编car*_*arl的帖子

使用BufferedWriter in flask whooshalchemy

嗨,我正在运行带有postgreSQL数据库的烧瓶应用程序.使用多个worker时,我得到LockErrors.我了解到这是因为嗖嗖搜索会锁定数据库

http://stackoverflow.com/questions/36632787/postgres-lockerror-how-to-investigate
Run Code Online (Sandbox Code Playgroud)

正如在这个链接中所解释的,我必须使用BufferedWriter ...我谷歌周围,但我真的无法弄清楚如何实现它?这是我的数据库设置方面的嗖

import sys
if sys.version_info >= (3, 0):
    enable_search = False
else:
    enable_search = True
    import flask.ext.whooshalchemy as whooshalchemy

class User(db.Model):
    __searchable__ = ['username','email','position','institute','id'] # these fields will be indexed by whoosh

    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(100), index=True)
    ...

    def __repr__(self):
        return '<User %r>' % (self.username)

if enable_search:
    whooshalchemy.whoosh_index(app, User)
Run Code Online (Sandbox Code Playgroud)

感谢carl非常感谢

编辑:如果没有能力并行访问烧瓶 - whosshsqlalchemy你有什么选择吗?

python whoosh flask-sqlalchemy flask-whooshee

7
推荐指数
1
解决办法
348
查看次数

获得特定标签后的文字与美丽的汤

我有一个类似的文字

page.content = <body><b>Title:</b> Test title</body>
Run Code Online (Sandbox Code Playgroud)

我可以获得Title标签

soup = BeautifulSoup(page.content)
record_el = soup('body')[0]
b_el = record_el.find('b',text='Title:')
Run Code Online (Sandbox Code Playgroud)

但我如何获得b标签后的文字?我想通过引用该元素而不是body元素来获取包含"Title:"的元素之后的文本.

html python beautifulsoup

7
推荐指数
1
解决办法
1万
查看次数

favicon.ico在flask应用中导致404错误

我在日志文件中发现了很多条目,这表明有人试图加载/favicon.ico和类似文件

GET - /favicon.ico
GET - /apple-touch-icon.png
GET - /apple-touch-icon-precomposed.png
Run Code Online (Sandbox Code Playgroud)

我在网上阅读了很多有关此问题的内容,但我无法摆脱它。这就是我的真实。首先,我将以下内容添加到我的头部标签中

<link rel="shortcut icon" href="/static/favicon/favicon.ico" type="image/x-icon">
Run Code Online (Sandbox Code Playgroud)

但是,即使我在标头中提供了此信息,似乎仍有一些浏览器不关心它,仍然调用/favicon.ico?因此,我认为仅将ico文件放到根目录并使用它完成它,但似乎不起作用?如果我打电话

http://localhost:5000/static/favicon/favicon.ico
Run Code Online (Sandbox Code Playgroud)

我到达图标,但是

http://localhost:5000/favicon.ico
Run Code Online (Sandbox Code Playgroud)

不起作用(给出404)?我清除了缓存,并在Chrome和Safari中进行了尝试,但在两种情况下都得到404?我真的很茫然。如果我将图像移到静态文件夹中并调用

http://localhost:5000/static/favicon.ico
Run Code Online (Sandbox Code Playgroud)

可以,但是根文件夹不行吗?我在这里想念什么?

html browser flask

6
推荐指数
1
解决办法
4250
查看次数

在moments.js中选择默认时区

我用以下方式包括moments.js

<script src="../static/js/moment-timezone-full.js"></script>
<script>
    $('.select_timezone').timezones();
</script>
Run Code Online (Sandbox Code Playgroud)

然后我在html中为我的用户提供时区选择

<div class="row">
    <div class="col-lg-4">
        <select class="select_timezone" name="select_timezone"></select>
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

我的问题是如何选择默认时区?我看到一个人可以做到这一点

moment.tz.setDefault(String);
Run Code Online (Sandbox Code Playgroud)

但我不明白这在我的设置中会如何工作?

编辑:我的实现只是遵循这个例子......

http://www.jqueryscript.net/time-clock/Easy-Timezone-Picker-with-jQuery-Moment-js-Timezones.html

真的没人知道解决这个问题吗?卡尔

编辑:我应该澄清为什么moment.tz.setDefault不起作用.当我添加该行

moment.tz.setDefault('America/Los_Angeles');
Run Code Online (Sandbox Code Playgroud)

我收到了javascript错误

Uncaught TypeError: Cannot read property 'setDefault' of undefined
Run Code Online (Sandbox Code Playgroud)

我包括在烧瓶大型教程中解释的时刻,意思是我

from flask.ext.moment import Moment
...
moment = Moment()
...
moment.init_app(app)
Run Code Online (Sandbox Code Playgroud)

在我的html模板中我做

{% block scripts %}
{{ moment.include_moment() }}
{% endblock %}
Run Code Online (Sandbox Code Playgroud)

moment-timezones包含在js文件中,如上所示.我忘了什么吗?我只是想设置选择的默认时区

timezone momentjs

5
推荐指数
1
解决办法
3746
查看次数

在flask中处理javascript输出后重新加载页面

我有一个 javascript 片段,它使用 ajax 将电子邮件地址发送到我的flask views.py 脚本。如果电子邮件不在我的数据库中,我想向该地址发送一条消息,或者以其他方式重新加载网站并显示该电子邮件地址的用户信息。这是我的 javascript 代码,它将数据发送到我的 views.py

<script type='text/javascript'>
    $(".send_invite_message").click(function(evt) {

        var Toemail = document.getElementById('To').value
        $.ajax({
            url: "/send_invitation_member",
            type: "GET",
            async: true,
            cache: false,
            dataType: 'json',
            contentType: "application/json; charset=utf-8",
            data: { email: Toemail}, 
            success: function(data) {
                ///do something
            },
        });
    });
</script>
Run Code Online (Sandbox Code Playgroud)

在烧瓶中,我现在可以选择发送电子邮件,如果数据库中已经存在电子邮件以重新加载站点

@app.route('/send_invitation_member', methods=['GET'])
def send_invitation_member():

    if request.method == 'GET':
        email = request.args.get('email')

        search_result = check database for email entry
        if search_result:
            return render_template('show_members.html')
        else:
            send message and return json object
Run Code Online (Sandbox Code Playgroud)

但是 ajax 脚本需要返回一个 json 对象,所以我不知道如何重新加载站点并显示用户信息。有什么方法可以直接在烧瓶中执行此操作,还是需要扩展我的 …

javascript python ajax flask

5
推荐指数
1
解决办法
2979
查看次数

利用浏览器缓存Nginx,重新加载页面时没有css

我正在努力遵循google pagespeed建议并利用浏览器缓存.为此,我将以下代码放入我的nginx.conf文件的服务器块中.

location ~*  \.(jpg|jpeg|png|gif|ico|css|js)$ {
    expires 365d;
}

location ~*  \.(pdf)$ {
    expires 30d;
}
Run Code Online (Sandbox Code Playgroud)

它似乎工作得很好,页面速度将我的分数从87/100提高到95/100.但是,当我单击我的网站的刷新按钮时,它似乎不再加载css文件?缓存不起作用吗?

我得到的错误信息是

Failed to load resource: the server responded with a status of 404 (Not Found)
Run Code Online (Sandbox Code Playgroud)

这是我的整个nginx.conf文件

worker_processes 1;

events {

    worker_connections 1024;

}

http {
    include /etc/nginx/mime.types;

    sendfile on;

    gzip              on;
    gzip_http_version 1.0;
    gzip_proxied      any;
    gzip_min_length   500;
    gzip_disable      "MSIE [1-6]\.";
    gzip_types        text/plain text/xml text/css
                  text/comma-separated-values
                  text/javascript
                  application/x-javascript
                  application/atom+xml;

    # Configuration containing list of application servers
    upstream app_servers {

        server 127.0.0.1:8080;
    }

    # Configuration …
Run Code Online (Sandbox Code Playgroud)

html css nginx browser-cache google-pagespeed

5
推荐指数
1
解决办法
4024
查看次数

sqlalchemy:分页未返回预期的元素数量

我将flask-sqlalchemy 与sqlite 数据库一起使用。我尝试获得 date1 以下的所有选票

sub_query = models.VoteList.query.filter(models.VoteList.vote_datetime < date1)
sub_query = sub_query.filter(models.VoteList.group_id == selected_group.id)
sub_query = sub_query.filter(models.VoteList.user_id == g.user.id)
sub_query = sub_query.subquery()

old_votes = models.Papers.query.join(sub_query, sub_query.c.arxiv_id == models.Papers.arxiv_id).paginate(1, 4, False)
Run Code Online (Sandbox Code Playgroud)

VoteList 的数据库模型如下所示

class VoteList(db.Model):

id = db.Column(db.Integer, primary_key=True)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
group_id = db.Column(db.Integer, db.ForeignKey('groups.id'))
arxiv_id = db.Column(db.String(1000), db.ForeignKey('papers.arxiv_id'))
vote_datetime = db.Column(db.DateTime)

group = db.relationship("Groups", backref=db.backref('vote_list', lazy='dynamic'))
user = db.relationship("User", backref=db.backref('votes', lazy='dynamic'), foreign_keys=[user_id])

def __repr__(self):
    return '<VoteList %r>' % (self.id)
Run Code Online (Sandbox Code Playgroud)

我确保上面的“old_votes”选择有 20 个元素。如果我使用 .all() 而不是 .paginate() 我会得到预期的 20 …

sql pagination sqlalchemy flask-sqlalchemy

5
推荐指数
1
解决办法
2897
查看次数

这超出了GitHub的文件大小限制

我在我的github存储库中有一些大文件,我试图添加/ commit/push但是push命令给出了以下错误

remote:错误:文件app_dump.sql是106.67 MB; 这超过了GitHub的文件大小限制为100.00 MB

所以我从我的存储库中删除了大文件(app_dump.sql)并再次添加/ commit/push.但我认为这不是正确的事情,因为每当我推动它仍然会尝试推送大文件?

$ git status
On branch master
Your branch is ahead of 'origin/master' by 4 commits.
  (use "git push" to publish your local commits)

nothing to commit, working directory clean
Run Code Online (Sandbox Code Playgroud)

所以现在每次推送都会得到相同的文件大小错误.如何从git中删除文件?我试过了

$ git rm app_dump.sql
fatal: pathspec 'app_dump.sql' did not match any files
Run Code Online (Sandbox Code Playgroud)

但这没有找到任何文件,因为我删除了它...谢谢卡尔

git github

5
推荐指数
2
解决办法
4998
查看次数

使用 git add * 时“已杀死:9”

我使用时出现错误

git add *
Run Code Online (Sandbox Code Playgroud)

这只是说

Killed: 9
Run Code Online (Sandbox Code Playgroud)

如果我再次输入 git add * 我得到

fatal: Unable to create 
'.git/index.lock': File exists.

Another git process seems to be running in this repository, e.g.
an editor opened by 'git commit'. Please make sure all processes
are terminated then try again. If it still fails, a git process
may have crashed in this repository earlier:
remove the file manually to continue.
Run Code Online (Sandbox Code Playgroud)

如果我删除 .git/index.lock 并再次输入 git add *,我会再次收到终止错误。

我检查了 stackoverflow 上的其他答案,例如

git add 命令失败并继续运行

但我不使用任何虚拟环境。

git github

5
推荐指数
1
解决办法
5729
查看次数

在 matplotlib 的子图中使用 xscale 和 yscale

我创建像这样的子图

fig, (ax1, ax2) = plt.subplots(1, 2, sharex='col', sharey='row')
Run Code Online (Sandbox Code Playgroud)

我用

ax1.plot(x, y)
ax2.plot(x, y)
Run Code Online (Sandbox Code Playgroud)

现在我想将轴格式化为

ax1.xscale('log')
ax1.yscale('log')
Run Code Online (Sandbox Code Playgroud)

但这导致

AttributeError: 'AxesSubplot' object has no attribute 'yscale'
Run Code Online (Sandbox Code Playgroud)

如何设置两个子图的轴格式?

python matplotlib

5
推荐指数
0
解决办法
3509
查看次数