Django style multiple apps with web.py not working - web.py

I run the example in the tutorial Django style multiple apps with web.py, but it does not working. The following is error message:
Traceback (most recent call last):
File "run.py", line 11, in <module>
delegate.run(mapping)
File "/home/siongui/dev/test/delegate.py", line 31, in run
web.run(handler, {})
AttributeError: 'module' object has no attribute 'run'
My web.py version is 0.37. Is there any idea on how to fix this? Thanks.
(subapp is not a option for me.)

There is some addition for the application module in 0.3 (http://webpy.org/docs/0.3/)
Applications. How to map urls to python code paths.
- Basic app. Map regexes to classes.
- Auto app. Have webpy keep track of the urls based on class name.
- Subdir app. Host multiple apps based on the sub-directory.
- Subdomain app. Host multiple apps based on the subdomain.
Following this cookbook should get things to work.
http://webpy.org/cookbook/subapp

It's even easier than the example provided at webpy.org.
Updating that Django style multiple apps example:
Keep wiki.py, and blog.py as specified in the example
don't bother with delegate.py hack: it's no longer needed.
change run.py to:
"""run.py"""
import web
import wiki
import blog
urls = ("/blog", blog.app_blog,
"/wiki", wiki.app_wiki,
"/(.*)", "index")
class index:
def GET(self, path):
return "other: " + path
app = web.application(urls, locals())
if __name__ == "__main__":
app.run()
The key change is the urls specified in run.py. If the second item is a string ("index" in the above example) then web.py expects that to be a class to handle the matching url ("/(.*)" in the example). This is the way webpy normally works.
However, if the second item is type application (blog.app_blog, for example), then the matching url (/blog) isn't a regex, it's a prefix & all incoming requests matching that prefix are handed to the related application (and the prefix is removed from the incoming request).

Related

Writing string to specific dir using chaquopy 4.0.0

I am trying a proof of concept here:
Using Chaquopy 4.0.0 (I use python 2.7.15), I am trying to write a string to file in a specific folder (getFilesDir()) using Python, then reading in via Android.
To check whether the file was written, I am checking for the file's length (see code below).
I am expecting to get any length latger than 0 (to verify that the file indeed has been written to the specific location), but I keep getting 0.
Any help would be greatly appreciated!!
main.py:
import os.path
save_path = "/data/user/0/$packageName/files/"
name_of_file = raw_input("test")
completeName = os.path.join(save_path, name_of_file+".txt")
file1 = open(completeName, "w")
toFile = raw_input("testAsWell")
file1.write(toFile)
file1.close()
OnCreate:
if (! Python.isStarted()) {
Python.start(new AndroidPlatform(this));
File file = new File(getFilesDir(), "test.txt");
Log.e("TEST", String.valueOf(file.length()));
}```
It's not clear whether you've based your app on the console example, so I'll give an answer for both cases.
If you have based your app on the console example, then the code in onCreate will run before the code in main.py, and the file won't exist the first time you start the activity. It should exist the second time: if it still doesn't, try using the Android Studio file explorer to see what's in the files directory.
If you haven't based your app on the console example, then you'll need to execute main.py manually, like this:
Python.getInstance().getModule("main");
Also, without the input UI which the console example provides, you won't be able to read anything from stdin. So you'll need to do one of the following:
Base your app on the console example; or
Replace the raw_input calls with a hard-coded file name and content; or
Create a normal Android UI with a text box or something, and get input from the user that way.

NameError: undefined local variable or method `request' for main:Object

I'm building a Sinatra app which will take a visitor's IP address and return the weather of that location. Following the Geocoder doc, it would seem that getting the IP is as simple as
city = request.location.city
Entering that into irb, however, just gives the NameError you see in the post title. Going what seems to me the logical route and attempting to create a new class upon which to use the request method results in another NameError - this time for the env hash taken by Rack::Request.new(env).
I can figure out how to build the thing on my own, but I'm stumped on this particular point, and assume that I am overlooking something not mentioned in the docs. What am I doing wrong that is causing request and env to remain undefined, and how do I go about correctly defining them?
It is as simple as that:
require "sinatra"
require "geocoder"
get "/" do
p request.location.city
"works"
end
Your problem is that you trying to run it in irb where you haven't the context of a request which creates and populates your request object. To play with it I would recommend just to run sinatra. Or use something like racksh, tux which gives you a something like irb but with all the context needed.

What does this line of code reference: "var io = require('../..')(server);"?

I followed this tutorial to create a chatroom using socket.io: https://github.com/socketio/socket.io.
I then created a page incorporating the chatroom (within the tutorial file structure) and now I am trying to export this into a different project...
I can't seem to do this without incorporating ALL the files from this tutorial.
I think I have narrowed down the issue to be this one line of code within the chat index.js page:
var io = require('../..')(server);
What does the above line reference to within the tutorial?
The error I get in my new project structure is this:
module.js:339
throw err;
^
Error: Cannot find module '../..'
Here is a link to the index.js file that has that line of code:
https://github.com/socketio/socket.io/blob/master/examples/chat/index.js
require() accepts a module name or a path to a module.
Per the node.js docs for require(), one of the argument options leads to this:
LOAD_AS_DIRECTORY(X)
1. If X/package.json is a file,
a. Parse X/package.json, and look for "main" field.
b. let M = X + (json main field)
c. LOAD_AS_FILE(M)
2. If X/index.js is a file, load X/index.js as JavaScript text. STOP
3. If X/index.json is a file, parse X/index.json to a JavaScript object. STOP
4. If X/index.node is a file, load X/index.node as binary addon. STOP
This would mean that ../.. is trying to go up the directory tree and load package.json, index.js, index.json or index.node from that directory.
You can read about the allowed syntax for require() here in the node.js docs.
FYI, there's a chat room demo on the socket.io site here.

Meteor, coffeescript files running on every page

I am just getting started with Meteor and have encountered something that isn't necessarily an issue but something that I just don't understand. I have the following code in a file called chat.coffee...
Meteor.setInterval ( ->
console.log "Hello " + roomName
Meteor.call('keepAlive', Meteor.user(), roomName)
return
), 5000
I originally was under the impression that coffee-script files only ran on their associated html files. This doesn't seem to be the case here as this code runs on every single page regardless of the file name. Is this the intended way things are supposed to work, and if so, is there a way to enforce that only certain code runs on certain pages.
One thing to mention is that this code is running in the client side folder.
On the client side, Meteor will associate your templates with their javascript functions and helpers based upon shared template names, but that is not inherently tied to your file names.
By way of example, if you have a template named "chat" in an html file as follows:
<template name="chat"></template>
Meteor will run scripts such as Template.chat.helpers({}) or Template.chat.events({}) only in connection with the "chat" template. But that is not dependent on your file naming conventions. It could be placed in a file name chat.js for organization and convention, but could equally well reside in a file named client.js or any other arbitrarily named .js file.
Similarly, your <template name="chat"> could reside in a file named chat.html, or client.html, or an arbitrary name of your choosing.
Your setInterval function is not tied to a specific template so it will run on every page, even if it resides in a file named chat.js.
Correct.
Meteor merges all your javascript ( via coffeescript ) and all the html, which it stores in its own special way. It merges all the html in heads and body etc into a page and serves that up, and it will then render templates as you specify.
To have a more "page" oriented app you can use something like iron router.

Pydev Nodebox: "AttributeError: 'NoneType' object has no attribute 'WIDTH'"

I am trying to create a graph of the connections between the users in my database using nodebox(ubuntu 12.04, python 2.7, django 1.3), but when I enter the following instructions, I get the error message underneath it:
"""
g.draw(weighted=False, directed=False, highlight=[], traffic=None)
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/IPython/core/interactiveshell.py", line 2721, in run_code
exec code_obj in self.user_global_ns, self.user_ns
File "<ipython-input-1-0a219395b690>", line 1, in <module>
g.draw(weighted=False, directed=False, highlight=[], traffic=None)
File "/home/raymond/Documents/exchange/cet/cet/graph/__init__.py", line 453, in draw
self.update()
File "/path/to/myproject//graph/__init__.py", line 416, in update
self.x = _ctx.WIDTH - max.x*self.d - min_.x*self.d
AttributeError: 'NoneType' object has no attribute 'WIDTH'
"""
How can I get nodebox to draw my graph? should I add a try/catch or if statement to graph/init.py in order to prevent object of type none from being written to? I know displaying more code would help solve the problem faster; I am running into formatting problem, but I will add code to the question as soon as I can.
Nodebox has a graph web page which is concise, informative and seemed what I need. As the file to download is on the same page it seemed ideal, BUT it fails to mention it is Nodebox 1 which is for Mac ...only!
Then a further rummage finds NodeBox OpenGL which has a new methodology and is cross platform, BUT it only lists compatibility with python 2.5-2.6 (so with 2.7 should be worth a try?) BUT installing the graphics util Pyglet failed the first time ....so more investigation required.
Sadly Nodebox 3 seems all different and haven't yet seen a comparable graph command
Looking back on the outcome
After achieving an install, I prototyped network viewer with a mouse selectable attribute viewer, low frame rate and a constant data set. A flavour of the interactiveness is demonstrated in this site
http://www.visualthesaurus.com/app/view?word=link
(try a click and drag on the central word)
But problems I needed to solve were how to
show a live data set consistently ie dealing with orphans,
transitions as branches you may be viewing may disappear from the
data
partitioning the viewing of large amounts of data ie view 2 to 20 nodes from several hundred items
displaying data without overlapping/obscuring or going off the display area
displaying differences when not on the current level
portability
...
In my case a periodically updated, structured html table with colours and zoom levels was the best solution