wxWidgets/wxPython: Migrating from htmlWindow to Webview - python

I have an app that uses htmlWindow and would like to migrate it to the new webview found in wx 2.9. However, I have learned there is no built-in method available to pass a JavaScript variable from the webpage back to the Python code. There is a RunScript method that allows one to send JavaScript to the page, but no method to retrieve an element id on a user click or any other user interaction.
My question is, is there any workaround to this? Is there any way to intercept, say, an alert call or anything else and get the data? The webview display is of not much value if one cannot receive data from user interaction.

As far as I'm aware the only way to get a return value from RunScript() is to use the page title hack.
e.g. somewhere in RunScript you set document.title to the value you wish to retrieve and get it into python with GetCurrentTitle(), if you wish you can reset the title after you have retrieved the data.
So if self.html is the webview
self.html.RunScript("""
//javascript goes here
// variable we wish to retrieve is called return_value
document.title = return_value
""")
r = self.html.GetCurrentTitle()
If you want to initiate it from within the webview it can be done (as suggested in the link Robin posted) by overriding the wxEVT_COMMAND_WEB_VIEW_NAVIGATING so that when it receives a custom url scheme, e.g. retrievedata://data/.... it retrieves the data from the url and does whatever you want with it, making sure you call evt.Veto() at some point. You can then pass any data you wish by simply calling a suitable url from within javascript.
Totally untested code below (just to give you an idea of how it can be done)
def OnPageNavigation(self, evt):
url = evt.GetUrl()
if url.startswith("retrievedata://data/"):
data = url[len("retrievedata://data/"):]
evt.Veto()
// Do whatever you want with the data
Alternatively you could use a combination of the two ideas and create a single url that when accessed calls GetPageTitle() and just make sure you set document.title before calling the page.

There was recently some discussion on the wx-users mail list and a suggestion for a workaround for things like this. I haven't tried it myself, but you may find it useful. See https://groups.google.com/d/topic/wx-users/Pdzl7AYPI4I/discussion

Related

Refreshing page on database modification

In the main page of my website there is a long list, <ul>. Each list item represents a model object. Within the list there are the objects attributes, for example its name etc, and also buttons to change those attributes. The list is loaded from the sqlite database. The issue is since each button changes data in the database, to display the changed data, the view function reloads the page on each button click. That is fine, except the list is very long, and is necessary to scroll down, and on each button click, the page is reloaded therefore goes to the very top of the list. This makes the webpage almost unusable, or at least very annoying to use.
Can someone recommend a workaround to this problem. Please let me know if my question is not clear
It looks like what you want to achieve is something that should be done with Javascript. It will be the simplest way of doing it! For instance, you can call the corresponding API when the user clicks on one of the buttons, and if the API returns 'OK' then you can just update the item with the changes you made (because if the server returned yes, then you can assume that the local version of the data is the same than the one on the server)

How i post data on search-Bar website using python script?

As all we know in web application we have get method and post data method.
Here my problem appear with post data.
For example i want to make my python code that access for search bar of website by insert same values and submit (the website button), then check for the page.
How the code gonna be then if there any documentation about this python concepts!
I am totally confused
Note : i am just beginner in python.
If the website relies on javascript, you're going to need to use something like Selenium which will emulate a typical browser and allow you to insert information onto a page and execute javascript commands.
If, however, the search bar simply posts data to a URL. You can determine that URL and then use requests to post the data and retrieve the result.
resp = requests.post('http://website/search', data = {'term':'value'})

web2py: link to execute a function without navigating to a different page (callback?)

Using web2py (2.8.2-stable+timestamp.2013.11.28.13.54.07), how do I construct a link in a View that executes a function, but does not navigate away from the current page?
The web2py OnLine book states that, for a call to the built-in helper A() used in a View: 'Instead of _href you can pass the URL using the callback argument . . . the effect of pressing the link will be an ajax call to "myaction" instead of a redirection'. (My understanding of 'redirection' is to navigate the browser to a different page). However, the web2py View source (inside a loop):
{{=A('why does this callback redirect?', callback=URL('PlayAnAlbum2', vars = dict(album_folder = album_folder, type = 'artists')))}}
does execute the desired function, but also redirects to either the View of the named function, or a page consisting of 'None' if the function doesn't return anything.
I would like a click on the link to only execute a function, and not load a different View (so no view is required and I expect nothing need be returned by the function except maybe whether the function encountered any errors). An example of the source that is generated is as follows:
<a data-w2p_disable_with="default" data-w2p_method="POST" href="/AlbumBrowser/default/PlayAnAlbum2?album_folder=Jaga+Jazzist%2F2003+The+Stix&type=artists" id="10aa76bd-c4bf-4701-a0da-1ebfed7847cc">why does this callback redirect?</a>
But I had expected something like the following (I made this up loosely based on code in this question so may not make sense):
why does this callback redirect?
I can confirm {{include 'web2py_ajax.html'}} is in the layout head and <script src="/AlbumBrowser/static/js/web2py.js" type="text/javascript"></script> is in the generated page source.
As is stated in the question and the web2py book, use callback=URL() instead of _href=URL() within A().
This definitely works in web2py 2.8.2.

Pass data in google app engine using POST

I'm trying to pass a variable from one page to another using google app engine, I know how to pass it using GET put putting it in the URL. But I would like to keep the URL clean, and I might need to pass a larger amount of data, so how can pass info using post.
To illustrate, I have a page with a series of links, each goes to /viewTaskGroup.html, and I want to pass the name of the group I want to view based on which link they click (so I can search and get it back and display it), but I'd rather not use GET if possible.
I didn't think any code is required, but if you need any I'm happy to provide any needed.
Links inherently generate GET requests. If you want to generate a POST request, you'd need to either:
Use a form with method="POST" and submit it, or
Use AJAX to load the new page.

Modify wiki page (Confluence) programmatically

I'd like to modify a wiki page (Confluence by Atlassian - JIRA editors) programmatically (in python). What I tried so far is to simulate user behaviour:
click on Edit button
change content of a textarea input
submit changes with Save button
Part 1 is ok since I have the URL corresponding to an edit of the page, part 2 (retrieval of the page and modification) is ok too, but I don't know how to achieve step 3... I'm using urllib2.
Thanks for your help !!
EDIT: XML-RPC is indeed the solution, this example does exactly what I want !
# write to a confluence page
import xmlrpclib
CONFLUENCE_URL = "https://intranet.example.com/confluence/rpc/xmlrpc"
CONFLUENCE_LOGIN = "a confluence username here"
CONFLUENCE_PASSWORD = "confluence pwd for username"
# get this from the page url while editing
# e.g. ../editpage.action?pageId=132350005 <-- here
PAGE_ID = "132350005"
client = xmlrpclib.Server(CONFLUENCE_URL, verbose = 0)
auth_token = client.confluence2.login(CONFLUENCE_LOGIN, CONFLUENCE_PASSWORD)
page = client.confluence2.getPage(auth_token, PAGE_ID)
# and write the new contents
page['content'] = "!!!your content here!!!"
result = client.confluence2.storePage(auth_token, page)
client.confluence2.logout(auth_token)
Note that confluence modifies your html code when you do this. It strips out scripts, styles and sometimes title attributes on elements for example. In order to get that stuff back in you then need to use their macro code.
Easiest way to do this is to edit the page in confluence and make it look like you want and then grab the page and do a print page['content'] to see what magical new stuff the atlassian people have decided to do to standard html.
This seems like the absolutely wrong way to go about it.
First off, Confluence has a plugin architecture which should allow you to manage content programmatically from the application itself without any kind of HTTP requests. Secondly, even if you don't want to, or can't, use the plugin API for some reason, the next obvious option is to use the SOAP/XML-RPC API.
There is no reason to actually mess with buttons and textareas unless you're trying to do some kind of end-to-end test that includes testing GUI (e.g. automated cross-browser testing).

Categories

Resources