pymongo


How do I call a function with pymongo using webpy?


I am trying to learn webpy so I can call some python programs written using pymongo from a browser window running Javascript. I am pretty new to web programming in general. Right now I am trying to start by simply adapting the sample tutorial files found here: http://kooneiform.wordpress.com/2010/02/28/python-and-ajax-for-beginners-with-webpy-and-jquery/
I already successfully ran the sample files in my browser window. Then I adjusted app.py to look like this:
import web
import datetime
import time
from collections import OrderedDict
from math import *
import sys
import operator
import pymongo
def make_text(string):
return string
urls = ('/', 'tutorial')
render = web.template.render('templates/')
app = web.application(urls, globals())
my_form = web.form.Form(
web.form.Textbox('', class_='textfield', id='textfield'),
)
def check_users(user_ids):
""" helper function for user_id-based queries """
connection = MongoClient()
db = connection.test
if user_ids == "all":
user_list = []
for user_id in db.user_ids.find():
user_list.append(user_id["user_id"])
user_ids = user_list
return user_ids
class tutorial:
def GET(self):
form = my_form()
return render.tutorial(form, "Your text goes here.")
def POST(self):
form = my_form()
form.validates()
s = form.value['textfield']
users = check_users(s)
return make_text(users)
if __name__ == '__main__':
app.run()
When I run it, I get "Name Error: MongoClient is not defined."
How do I get webpy to open a Mongo connection so I can run queries from a browser?
You need to import any module/class you intend to use. In this case replace
import pymongo
with
from pymongo import MongoClient
Alternatively, because you are already importing the pymongo module, you could simply just do
connection = pymongo.MongoClient()

Related Links

Pymongo error : 'Cursor' object has no attribute 'find' , how to filter it?
Spark Streaming: foreachRDD insert into mongoDB using python?
how to find right version of bson from pip for pymongo/mongoengine
How to enable `projection` in pymongo query class `find()`?
Variables don't hold value for very long pymongo ipython
pymongo upgrade to 3.0.3 causes this statement to fail
UUID to NUUID in Python
how to test for empty mongodb query in pymong
read video file from mongodb with pymongo
Get OperationFailure error with PyMongo 2.7.1
Inserting string as regular string in mongodb
Mongos + Pymongo 2.5 ==>No suitable hosts found
pymongo collection add item using update
How to convert objectid to string
Conditional atomic mongo remove with different results
python script to connect to mongodb master

Categories

HOME
azure-functions
matrix
opencart
livecode
hdfs
magento-2.0
mathematical-optimization
cypher
elk-stack
azure-data-lake
ag-grid
azure-graph-api
angular4
sslhandshakeexception
google-awareness
sql-injection
temperature
vertica
exe
raml
bsd
hdl
intel
sybase-ase
tortoisehg
credit-card
locks
epplus
caesar-cipher
quickcheck
xmlunit
directfb
des
textmate2
hidden-markov-models
selenide
suds
oauth2-playground
sendinput
nomad
autorelease
intellij-lombok-plugin
openstack-glance
google-news
stateless-session-bean
1010
case-when
http4s
adobe-reader
annotatorjs
yui3
jsonix
fiber
m4
helm
smart-tv
highest
sqlbase
gluon-desktop
espresso
gql
wlanapi
ngcordova
replicaset
compositetype
kotlin-android-extensions
camus
signed
mutators
findersync
adhoc-polymorphism
configurable-product
dc
producer
squeezebox
accessory
csslint
forceclose
meteor-collections
lsa
proxy-server
indesign-server
oscilloscope
ui-design
jbox2d
contextswitchdeadlock
system-requirements
clearinterval
image-scanner
platform-independent
algol68
netstream
asio
database-deadlocks
regression-testing
projectpier
inversion
activex-exe
synthesizer

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App