In flask programming, people usually use 'url_for' such as {{url_for = 'some url'}}
This way have to make URL(#app.route), template(HTML) and map each other.
But, I just want to send email when I click submit button in HTML modal.
For this, There is no page reloading. To do this, I think I have to connect between button and python function without URL, return(response)
I wonder that how to make it, help me please, I'm beginner in flask programming.
You can't trigger anything on the server without making a request to a URL.
If you don't want the page to reload, you can either redirect back to the original page after your action is finished, or you can use Ajax to make the request without changing the page; but the request itself is always to a URL.
Related
From this question, the last responder seems to think that it is possible to use python to open a webpage, let me sign in manually, go through a bunch of menus then let the python parse the page when I get where I want. The website has a weird sign in procedure so using requests and passing a user name and password will not be sufficient.
However it seems from this question that it's not a possibility.
SO the question is, is it possible? if so, do you know of some example code out there?
The way to approach this problem is when you login normally have the developer tools next to you and see what the request is sending.
When logging in to bandcamp the XHR request that's being sent is the following:
From that response you can see that an identity cookie is being sent. That's probably how they identify that you are logged in. So when you've got that cookie set you would be authorized to view logged in pages.
So in your program you could login normally using requests, save the cookie in a variable and then apply the cookie to further requests using requests.
Of course login procedures and how this authorization mechanism works may differ, but that's the general gist of it.
So when do you actually need selenium? You need it if a lot of the things are being rendered by javascript. requests is only able to get the html. So if the menus and such is rendered with javascript you won't ever be able to see that information using requests.
I have written a python function using flask framework to process some data submitted via a web form. However I don't want to re-render the template, I really just want to process the data and the leave the web form, it the state it was in, when the POST request was created. Not sure how to do this ... any suggestions ?
If you want the user to stay in place, you should send the form using JavaScript asynchronously. That way, the browser won't try to fetch and render a new page.
You won't be able to get this behavior from the Flask end only. You can return effectively nothing but the browser will still try to get it and render that nothing for the client.
I am trying to automate Internet Explorer interaction with a website using Python. I am using the IEC library to do this.
The website is set to trigger an action whenever a username is selected from a dropdown menu. The html code snippet is as follows:
<select name="ClientID" tabIndex="1001" title="Select a client" class="client-select focus-default need-focus-object valid" style="width: auto;" onchange="localChangeClient()">
<option value="0">
<option value="2629049">
I am able to programmatically set the user name in the dropdown list, but this does not trigger the "onchange" event. I have read some other references (links below) that indicate I should look for the AJAX request and trigger it directly, but I am not sure how to do this.
Trigger Javascript event on remote website with Python
on Change of dropdown list showld call the php function
I gather that 'onchange' triggers a call to a function called localChangeClient(). How do I call / trigger that function directly from Python IEC?
From the examples I linked, it seems an AJAX request would be made inside that localChangeClient function - is that right? How do I know what the AJAX request to call is, given that I cannot see the actual function details?
EDIT: The following describes continued efforts / developments. I think I just need to know how to call an AJAX URL directly
So I was reading more about AJAX requests. I found out I could use Firebug (Console window) to tell me the particular AJAX url that gets called for the onchange event. Firebug told me the following (snipped for clarity) when I changed the dropdown menu:
POST https://example.com/MyBasket/MyBasket.asp?update=Update%20Basket&ClientID=2629049,0&SDT=417.577989&SCheck=49913&Checkout=&ClientID=2629049&ClientID=0&CurrentPageUrl=&FullPage=false&MyBasketPage=1&OnClickScheduleCheckboxName=&OverlayRequest=true&TransactionStarted=0&ajax=true&cbxRegQuantity=1&cbxRegQuantity=1&update=no
So I think I am on the right track. But now I don't know how to trigger that url from Python. If I simply "ie.Navigate(url)", it does not seem to work. It takes me to a non-formatted window, and the dropdown menu is still not selected.
I checked a Microsoft support site (http://support.microsoft.com/kb/167658), which talked about encoding the request data as bytes etc, but I was unclear if that would apply here.
I would really appreciate if someone could provide some details on how to "do the AJAX request directly"
Use Python's requests library to make the call to that URL you found. You'll also need to find the body and any custom HTTP headers of that POST request in the Firebug console in order to replicate it in your script.
res = requests.post(<url>, data=<request-body>, headers=<headers>)
I am using Python and a javascript or php sdk.
To obtain the access_token - I follow the steps indicated on the docs page (https://developers.facebook.com/docs/authentication/). I pass the redirect url to dialog/oauth and obtained the access_token. Once this is done, all output html being sent back to the browser gets rendered into to a new page, leaving the facebook iframe/canvas. (fyi, all output is done thru the usual 'self.response.out.write' function call).
Seems the PHP sdk hides this and I can't find a way to get : http://www.facebook.com/dialog/oauth?client_id=%s&redirect_uri=%s" dialog to send the output from the redirected url to the iframe/canvas that triggered the application. This is a 'Page Tab' app (not a 'App on Facebook') so have set the "Page Tab Name" and "Page Tab Url" on the basic apps config page.
I have not implemented session yet and I am wondering if that is necessary to pass the iframe target as a state variable and have it passed back along with the redirect to the uri.
I have searched many posts/etc and no luck and any help would be much appreciated !!
Tab Page Application undocumented steps :
The confusing part is the CANVAS_PAGE_URL in the example. This needs to be the web-hosted app URL (e.g. https://www.appname.appspot.com/).. This not clearly defined..
If access_token and the user_id is not found on the signed_request an auth dialog needs to be done (as per the page documentation). This needs to be done thru the script top.location.ref to endure that it launches as a dialog. This goes to a new page overwriting the canvas (or the fan-page) that triggered the app.
When the user allows the permissions on the app, the app is called thru the tab-page-canvas-url?code=".....". At this point, a redirect needs to be done (which is not documented anywhere. I had to look at the php sdk code to figure this out (fbmain.php line 17) (redirect() in python and header() php). The redirect needs to take the url for the app on the fan page : http://www.facebook.com/FAN_PAGE_NAME?sk=app_nnnnnnn
It took many hours of research and digging to understand this and hope it helps (I see a lot of questions raised around page breaking out of the iframe/canvas) and the basic problem is the extra redirect step that FB does not document anywhere...
(mail me and I am happy to share python code that is now all working nicely)
It is stated elsewhere, but to make clear: the reason your app breaks out of facebook is because the authentication dialogs get away from the original apps.facebook.com url.
This may only happen with extended permissions as the new permissions screen is two pages instead of one.
Once the authorization process is complete, the browser is redirected to the fully qualified app url on your server.
The "fix" is to send the browser back to the Facebook app using it's http://apps.facebook.com/appname address.
[that doesn't seem like good "flow" to most people, but that is how it is right now. I think there may be a different route by using the "Authenticated Referrals" on the "Auth Dialog" page of the apps, but haven't used it yet]
I use the PHP SDK and here is what I do:
Check for the "state" request parameter when your redirct_url is called after authorization. Some people had suggested using the "code" parameter, but I do not see it being returned.
''
// after completing the first authorization, the redirect url may send users away from Facebook to the redirect url itself.
//This php code redirects them back to the app page
if (isset($_GET['state'])){ header("Location: http://apps.facebook.com/appname']); exit; }
If you know a better way, please let me know!
As far as I know, for a new request coming from a webapp, you need to reload the page to process and respond to that request.
For example, if you want to show a comment on a post, you need to reload the page, process the comment, and then show it. What I want, however, is I want to be able to add comments (something like facebook, where the comment gets added and shown without having to reload the whole page, for example) without having to reload the web-page. Is it possible to do with only Django and Python with no Javascript/AJAX knowledge?
I have heard it's possible with AJAX (I don't know how), but I was wondering if it was possible to do with Django.
Thanks,
You want to do that with out any client side code (javascript and ajax are just examples) and with out reloading your page (or at least part of it)?
If that is your question, then the answer unfortunately is you can't. You need to either have client side code or reload your page.
Think about it, once the client get's the page it will not change unless
The client requests the same page from the server and the server returns and updated one
the page has some client side code (eg: javascript) that updates the page.
You definitely want to use AJAX. Which means the client will need to run some javascript code.
If you don't want to learn javascript you can always try something like pyjamas. You can check out an example of it's HttpRequest here
But I always feel that using straight javascript via a library (like jQuery) is easier to understand than trying to force one language into another one.
To do it right, ajax would be the way to go BUT in a limited sense you can achieve the same thing by using a iframe, iframe is like another page embedded inside main page, so instead of refreshing whole page you may just refresh the inner iframe page and that may give the same effect.
More about iframe patterns you can read at
http://ajaxpatterns.org/IFrame_Call
Maybe a few iFrames and some Comet/long-polling? Have the comment submission in an iFrame (so the whole page doesn't reload), and then show the result in the long-polled iFrame...
Having said that, it's a pretty bad design idea, and you probably don't want to be doing this. AJAX/JavaScript is pretty much the way to go for things like this.
I have heard it's possible with AJAX...but I was
wondering if it was possible to do
with Django.
There's no reason you can't use both - specifically, AJAX within a Django web application. Django provides your organization and framework needs (and a page that will respond to AJAX requests) and then use some JavaScript on the client side to make AJAX calls to your Django-backed page that will respond correctly.
I suggest you go find a basic jQuery tutorial which should explain enough basic JavaScript to get this working.