使用flask服务器从浏览器查询MongoDB [英] Querying MongoDB from browser using flask server

查看:405
本文介绍了使用flask服务器从浏览器查询MongoDB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个交互式可视化工具,让用户查询数据,然后使用D3,Flask作为服务器,MongoDB作为数据库进行可视化。我的问题是如何查询用户输入的MongoDB,并将其呈现给服务器。



谢谢。

  • 安装pymongo(python软件包)
  • / li>
  • 然后使用以下命令创建pymong的一个实例:


    lockquote
    $ pre> from pymongo import MongoClient
    client = MongoClient(MONGO_URL)
    db = getattr(client,DATABASE_NAME)


    4。然后你可以使用下面的查询:



     > documents = db.your_collection.find()
    > db.your_collection.insert({'name':'Nabin Khadka'})

    这些python代码可以被包装在一个方法下的views.py文件中。像:

      @ app.route('/ test')
    def test():
    #以上代码
    返回jsonify(response_dictionary)

    然后运行应用程序,我们可以调用从浏览器下面的URL:

      https:// url_to_server:port / test 


    I am building an interactive visualization tool that lets users query data which is then visualized using D3, Flask as the server and MongoDB as the database. My question is how do i query the MongoDB with the user input and render this to the server.

    Thanks.

    解决方案

    1. Install the mongodb and run its server (in terminal type mongo)
    2. Install pymongo (python package)
    3. Then create an instance of the pymong using:

    from pymongo import MongoClient
    client = MongoClient(MONGO_URL)
    db = getattr(client, DATABASE_NAME)
    

    4. Then you can query using following:

    > documents = db.your_collection.find()
    > db.your_collection.insert({'name': 'Nabin Khadka'})
    

    These python code can be wrapped in views.py file under a method. Like:

    @app.route('/test')
    def test():
       # All above code
       return jsonify(response_dictionary)
    

    Then running the app, we can call the following url from browser:

    https://url_to_server:port/test
    

    这篇关于使用flask服务器从浏览器查询MongoDB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆