小编Tar*_*gar的帖子

Flask:单击按钮下载csv文件

我刚开始使用Flask/Python.我想要实现的是我的HTML中有一个下载按钮,它调用以下函数:

function downloadPlotCSV() {
        $.ajax({
            url: "/getPlotCSV",
            type: "post",
            success: function(data) {
                dataPlot = JSON.parse(data);
                console.log(dataPlot);
            }
        });
    }
Run Code Online (Sandbox Code Playgroud)

不完整的烧瓶代码是:

@app.route('/getPlotCSV', methods = ['POST'])
def plotCSV():
    data = open("outputs/Adjacency.csv")
Run Code Online (Sandbox Code Playgroud)

我面临的问题是我找不到下载此csv文件或将其作为JSON字符串返回的方法,因此我可以使用Javascript下载它.知道如何将其作为JSON发送或者通过Flask本身下载它吗?什么是最好的方式?

javascript python csv json flask

40
推荐指数
2
解决办法
4万
查看次数

filter:notarray预期的数组但收到:0

调节器

    @RequestMapping(value = "/graphs", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public Collection<Graph> getSkeletonGraph()
    {
        log.debug("REST request to get current graphs");
        return graphService.getSkeletonGraphs();
    }
Run Code Online (Sandbox Code Playgroud)

角度呼叫

    $scope.graphs = [];
    Graph.getGraphs().$promise.then(function(result)
    {
        $scope.graphs = result;
    });



    angular.module('sampleApplicationApp').factory('Graph', function($resource)
     {
      return {
        getGraphs: function() {
           return    $resource('api/graphs/:id').query();
      }
     };
    })
Run Code Online (Sandbox Code Playgroud)

我不知道为什么使用过滤器我得到了例外.

也看了角度文档https://docs.angularjs.org/error/filter/notarray 我的结果是数组,但不知道为什么我得到这样的异常.

来自后端的示例结果我得到了.

[{"id":"135520b0-9e4b-11e5-a67e-5668957d0149","area":"Bingo","models":[],"enumerateds":[]},{"id":"0db925e0-9e53-11e5-a67e-5668957d0149","area":"jin","models":[],"enumerateds":[]},{"id":"7a717330-9788-11e5-b259-5668957d0149","area":"Product","models":[],"enumerateds":[]},{"id":"402d4c30-980f-11e5-a2a3-5668957d0149","area":"fgfgfg","models":[],"enumerateds":[]},{"id":"404b77b0-9e53-11e5-a67e-5668957d0149","area":"olah","models":[],"enumerateds":[]},{"id":"cd071b10-9e52-11e5-a67e-5668957d0149","area":"lolo","models":[],"enumerateds":[]},{"id":"d9808e60-9710-11e5-b112-5668957d0149","area":"catalog","models":[],"enumerateds":[]},{"id":"2aaca9f0-97e2-11e5-91cd-5668957d0149","area":"btg","models":[],"enumerateds":[]},{"id":"955e9ed0-978c-11e5-93fd-5668957d0149","area":"promotions","models":[],"enumerateds":[]},{"id":"1e441d60-980f-11e5-a2a3-5668957d0149","area":"hjuhh","models":[],"enumerateds":[]},{"id":"fb96dfe0-978d-11e5-93fd-5668957d0149","area":"voucher","models":[],"enumerateds":[]}]
Run Code Online (Sandbox Code Playgroud)

HTML

<li ng-repeat="g in graphs track by $index | filter:searchText"></li>
Run Code Online (Sandbox Code Playgroud)

javascript angularjs angularjs-directive angularjs-scope angularjs-ng-repeat

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

Pymongo/bson:将python.cursor.Cursor对象转换为serializable/JSON对象

MongoDb和Python(webapp2)新手.所以,我从mongodb数据库中获取了一些数据.但我无法使用json.dumps返回的数据.这是我的代码:

exchangedata = db.Stock_Master.find({"Country": "PHILIPPINES"}, {"_id" : 0})        
self.response.write(json.dumps(exchangedata)) 
Run Code Online (Sandbox Code Playgroud)

这会引发错误:

TypeError: pymongo.cursor.Cursor object at 0x7fcd51230290 is not JSON serializable
Run Code Online (Sandbox Code Playgroud)

类型exchangedatapymongo.cursor.Cursor.如何将其转换为json对象?

python json mongodb pymongo webapp2

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

MongoDB:查询名称中包含空格的键

我想从MongoDB集合中检索仅某些键的值.

但是,该系列有一些键,其名称中有一个"空格",如:

"Parent":{"key1": //some string,
          "key2": //some string,
          "key 3": //some string}
Run Code Online (Sandbox Code Playgroud)

我知道这是一个错误的方法,因为理想情况下,键名中不应该有空格,但我如何查询此键?我正在使用Python和PyMongo.

对于普通键,我可以这样做:

db.coll_name.find({"key": "India"}, {"_id": 0, "Parent.key1": 1, "Parent.key2": 1})
Run Code Online (Sandbox Code Playgroud)

那么如何在上述查询的第二个参数中使用"Parent ['key 3']"键呢?有没有办法实现这个目标?

这是返回数据的查询(工作):

db.coll_name.find({}, {"Parent.key1": 1, "_id": 0})
Run Code Online (Sandbox Code Playgroud)

这是不返回数据的查询:

db.coll_name.find({}, {"Parent['key 3']": 1, "_id": 0})
Run Code Online (Sandbox Code Playgroud)

python mongodb pymongo mongodb-query

10
推荐指数
1
解决办法
6350
查看次数

AngularJS:ng-show vs display:none

我有一个用例,我必须使用CSS隐藏HTML元素,如下所示:

HTML:

<div class="item">
</div>
Run Code Online (Sandbox Code Playgroud)

CSS:

.item {
    display: none;
}
Run Code Online (Sandbox Code Playgroud)

但是,我需要在页面加载后使用ng-show切换元素的可见性,如下所示:

<div class="item" ng-show="show_element">
</div>
Run Code Online (Sandbox Code Playgroud)

但是,即使$scope.show_element设置为true,元素也不会变得可见,也就是说,css属性会覆盖AngularJS的ng-show.有没有办法让ng-show更优先?

此外,您可能认为我$scope.show_element最初可以保持虚假隐藏它.但是在这种情况下,当页面加载时,我会很快看到元素,这从UX的角度来看并不好.

javascript css angularjs ng-show

10
推荐指数
2
解决办法
2万
查看次数

在 SVG 元素中附加包含一些 HTML 的 foreignObject

有没有办法将foreignObject包含一些简单 HTML的元素附加到 svg 元素?这是我试过的代码:

 var foreignObject = document.createElement('foreignObject');
 foreignObject.setAttribute('height', '300');
 foreignObject.setAttribute('width', '300');
 var div = document.createElement('div');
 div.innerHTML = 'Hello World';
 foreignObject.appendChild(div); 
 svg.appendChild(foreignObject); //svg is an already created svg element containing a d3 chart
Run Code Online (Sandbox Code Playgroud)

但是即使在此代码执行之后 svg 也保持不变。还有什么我需要做的吗?

javascript svg d3.js

9
推荐指数
2
解决办法
6937
查看次数

AngularJS:为什么不在控制器中编写逻辑?

请原谅我,如果这听起来很愚蠢,但我现在已经使用AngularJS一段时间了,我看到有人告诉我将我的逻辑包装在一个指令(或服务?)而不是我的控制器中,只保留我控制器中的绑定.除了指令的可重用性方面还有其他原因吗?

到目前为止,我还没有真正理解为什么会这样.不写指令会带来很多开销吗?我没有遇到任何在我的控制器中编写逻辑的问题,而且很容易.这种方法的缺点是什么?

standards angularjs angularjs-directive angularjs-controller

8
推荐指数
1
解决办法
6569
查看次数

控制器性能比较中的自定义过滤器与过滤器功

假设我有一个5000个对象的数组(带有布尔值),我必须ng-repeat在模板中:

$scope.arr = [
    {
        "value": true
    },
    {
        "value": false
    },
    {
        "value": false
    }
    //and so on
]
Run Code Online (Sandbox Code Playgroud)

现在,我想ng-repeated根据动态变量过滤这个数组,比如'show_filter',我在其他地方设置.

如果'show_filter'设置为'all',我想显示所有对象.如果它设置为false(布尔值),那么我想显示"value"键设置为false的对象.当'show_filter'设置为true时也是如此.

所以,有两种方法:

1.构建自定义过滤器:

我会为过滤任务编写一个自定义过滤器,如下所示:

过滤:

app.filter('filterArr', function() {
    return function(arr, show_filter) {
        var filtered_arr = [];
        if(show_filter != 'All') { //if show_filter is a boolean value
            for(var i = 0; i < arr.length; i++) {
                if(arr[i].value == show_filter) { 
                    filtered_arr.push(arr[i]);
                }
            }
            return filtered_arr;
        }
        else {
            return arr; //return the entire array if show_filter …
Run Code Online (Sandbox Code Playgroud)

javascript performance filter angularjs angularjs-filter

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

ImportError:Elastic Beanstalk中没有名为django.core.wsgi的模块

我开始使用AWS的Elastic Beanstalk.

我按照本教程部署Django/PostgreSQL应用程序.

我在"配置数据库"部分之前做了所有事情.部署也很成功,但我收到内部服务器错误.

这是日志中的追溯:

 mod_wsgi (pid=30226): Target WSGI script '/opt/python/current/app/polly/wsgi.py' cannot be loaded as Python module.
[Tue Sep 15 12:06:43.472954 2015] [:error] [pid 30226] [remote 172.31.14.126:53947] mod_wsgi (pid=30226): Exception occurred processing WSGI script '/opt/python/current/app/polly/wsgi.py'.
[Tue Sep 15 12:06:43.474702 2015] [:error] [pid 30226] [remote 172.31.14.126:53947] Traceback (most recent call last):
[Tue Sep 15 12:06:43.474727 2015] [:error] [pid 30226] [remote 172.31.14.126:53947]   File "/opt/python/current/app/polly/wsgi.py", line 12, in <module>
[Tue Sep 15 12:06:43.474777 2015] [:error] [pid 30226] …
Run Code Online (Sandbox Code Playgroud)

python django amazon-web-services amazon-elastic-beanstalk

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

$ stateParams将值转换为字符串

我在用UI-Router.这是我通过ui-sref以下路线选择的州之一:

.state("community", {
    url: "/community/:community_id",
    controller: "CommunityCtrl",
    templateUrl: "/static/templates/community.html"
})
Run Code Online (Sandbox Code Playgroud)

在我进入"社区"状态的一个模板中:

<div ui-sref="community({community_id: community.community_id})"></div>
Run Code Online (Sandbox Code Playgroud)

这是我的社区对象:

{
    name: 'Community name',
    community_id: 11 //int, not a string
}
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,关键字'community_id'包含一个int值而不是字符串值.但是,当我通过以下方式访问此参数时$stateParams:

community_id: "11" //string
Run Code Online (Sandbox Code Playgroud)

我为什么要收到一个字符串?

javascript angularjs angular-ui-router ui-sref

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