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

Conditional atomic mongo remove with different results
python script to connect to mongodb master
flask-admin and pymongo search
Running code when mongoengine loads a document
Can't replace mongo document
How do I call a function with pymongo using webpy?
python motor offers fetch_next exception on aggregation
python motor mongo cursor length or peek next
PyMongo check if update succeeded
insert and query an OrderedDict in MongoHQ
Return .str of ObjectID using pymongo
Selecting the lowest/highest with find_one in Pymongo
Safe use of Pymongo's find_one
Pymongo $in + $regex
How can I get pymongo to always return str and not unicode?
SON Manipulator in mongo queries

Categories

HOME
hpoo
plesk
svn
windows-8.1
tcsh
informatica
semantic-web
autocomplete
tcp
mirc
lvm
material-components
apache-kafka-connect
wagtail
angular2-template
specifications
anchor
gtk
lapack
smooks
fullcalendar-scheduler
live-streaming
smartgwt
dhcp
android-5.1.1-lollipop
tomcat8
xsl-fo
bluemix-mobile-services
error-logging
qa
acl
rxjs5
sybase-ase
strophe.js
openbravo
npoi
spring-annotations
sales
landscape
bluemixtools
cloudera-quickstart-vm
strapi
unification
game-center
slideshow
grails-spring-security
klee
grails3.2.0
ejml
yoast
spooler
notesview
wysihtml5
dredd
datediff
deedle
lvalue
qtextedit
haraka
trust
webgrind
pack
m4
ui4j
gdl
deeplink
gluon-desktop
wso2ml
explain
r.java-file
mutators
mojo
uno
operands
famo.us
adhoc-polymorphism
global-scope
jms-serializer
typesafe-stack
assetic
mousemotionlistener
non-ascii-characters
xs
statechart
jquery-cycle
significant-digits
spark-view-engine
pcspim
java.lang.class
servercontrol
delphi-prism
snackjs
botnet
database-deadlocks
servlet-container
channelfactory
git-log
ninject-interception
icicles
principles
aspbutton
gtktextview
objectinstantiation
community-server

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App