Strip http protocol from url before display in web2py? - python

When using IS_URL validator web2py prepends http to the stored value. Now when forming a link in my view I'd like to remove the http and trailing slash.
<div>{{=A('<what goes here?>', _href=business.website)}}</div>
That is, given a url such as:
http://www.example.com/
I want the anchor text to be
www.example.com (or example.com)
I understand I can do this via standard python using urlparse.urlsplit, however wondering if web2py provides this functionality?
If using pure python is the best way to do this, where should the code to strip the url be? View? Controller?

I understand I can do this via standard python using urlparse.urlsplit, however wondering if web2py provides this functionality?
No, web2py does not provide that functionality, so just use standard Python.
If using pure python is the best way to do this, where should the code to strip the url be? View? Controller?
Your choice. If you do it in the view, you can avoid having to pass an additional variable from the controller.
Note, you can do IS_URL(prepend_scheme=None) if you want to prevent the validator from prepending the "http" (you can also set prepend_scheme to an alternative, such as "https").

Related

Docusign API: I want to attach some PDF to the envelope as appendix. Which API do I use?

I need to upload some file-like objects to docusign as appendix. There's no action needed to be done by any signer. They are just there as some additional info.
I found this which details creating one or more documents to an envelope, but at the same time I found this which also creates attachment. I don't know which one is the one I need. In addition, there's also this which adds more complexity to this issue.
DocuSign has listed both in here , which is even more confusing
I'm simply using python's requests library to make the call, but I don't know which one is the correct one to make.
To me it seems like the "appendix" is just a document. You won't have any tabs (signing indicators) in that document, but it's still a document.
You can either use the PUT API to add a new document, or if you already creating the envelope using the API, the initial POST can be used to add all the documents you need.
Use this API call for updating a document
PUT /v2.1/accounts/{accountId}/envelopes/{envelopeId}/documents

Specifying doc-id and plot-id (i.e. the URL) for output_server()

I would like to be able to specify the URL where output_server publishes my app in bokeh-server (as an example, I am trying to do this in the animate_widgets.py example that Christine Doig presented in Scipy2015).
I am already running bokeh-server in a separate terminal. When I run my app with output_server, is there any way to specify the URL where the app will be rendered?
It seems to currently follow the syntax:
http://localhost:5006/bokeh/doc/some-doc-id/some-plot-id
but I don't see the ability to specify those fields <doc-id> and <plot-id> with output_server (documentation for output_server below).
Is there any way to specify where exactly (URL-wise) I want the app to be published?
Note that just entering the string http://localhost:5006/bokeh/doc/some-doc-id/some-plot-id as URL for output_server() does not work.
The short answer is not really. Those URLs are meant to be unambiguous and to avoid collisions. Letting users choose their own URLs would be fairly unworkable in the general multi-user scenario. But that's OK, what you really probably want to is to embed a Bokeh server plot in your own document (as opposed to just linking to the bare page that has the plot and nothing else). This you can accomplish easily with server_session:
https://docs.bokeh.org/en/latest/docs/user_guide/embed.html#bokeh-applications
Edit: I won't actually say its impossible, but it's far enough outside normal usage that I don't know offhand how you could accomplish it, and even if you could it would probably not be advisable for several reasons.

Getting a URL with Python

I'm trying to do something similar to placekitten.com, wherein a user can input two strings after the base URL and have those strings alter the output. I'm doing this in Python, and I cannot for the life of me figure out how to grab the URL. In PHP I can do it with query string and $_REQUEST. I can't find a similar method in Python that doesn't rely on CGI.
(I know I could do this with Django, but that's serious overkill for this project.)
This is just by looking at the docs but have you tried it?
cherrypy.request.path_info
The docs say:
The ‘relative path’ portion of the Request-URI. This is relative to the script_name (‘mount point’) of the application which is handling this request.
http://docs.cherrypy.org/stable/refman/_cprequest.html#cherrypy._cprequest.Request.path_info

Does django support hashbang in its URL?

Does django support urls having #! in it.Say I have ursl like
http://example.com/foo/!#/bar
Any regex for that?
Note: http://example.com/foo/ and http://example.com/foo/#!/bar are different URLs and having different corresponding Views.
url(r'^(?P<#!>.+)/bar/$', 'my_view', name='my-view'), throwing bad character in group name
No. Anything after the # is not even sent to your webserver. If you want to make interactivity using the anchored (#) url styles, you need to go look at ajax libraries like jquery.
This is the "evil standard" way of denoting AJAX URLs. For a better description of the topic you should referer to this document http://code.google.com/web/ajaxcrawling/ which describes both what they are and how then to make URL handlers on your site handle them.

Scripting HTTP more effeciently

Often times I want to automate http queries. I currently use Java(and commons http client), but would probably prefer a scripting based approach. Something really quick and simple. Where I can set a header, go to a page and not worry about setting up the entire OO lifecycle, setting each header, calling up an html parser... I am looking for a solution in ANY language, preferable scripting
Have a look at Selenium. It generates code for C#, Java, Perl, PHP, Python, and Ruby if you need to customize the script.
Watir sounds close to what you want although it (like Selenium linked to in another answer) actually opens up a browser to do stuff. You can see some examples here. Another browser based record + playback approach system is sahi.
If your application is uses WSGI, then paste is a nice option.
Mechanize linked to in another answer is a "browser in a library" and there are clones in perl, Ruby and Python. The Perl one is the original one and this seems to be the way to go if you don't want a browser. The problem with this approach is that all the front end code (which might rely on JavaScript), won't be exercised.
Mechanize for Python seems easy to use: http://wwwsearch.sourceforge.net/mechanize/
My turn : wget or perl with lwp. You'll find example on the linked page.
If you have simple needs (fetch a page and then parse it), it is hard to beat LWP::Simple and HTML::TreeBuilder.
use strict;
use warnings;
use LWP::Simple;
use HTML::TreeBuilder;
my $url = 'http://www.example.com';
my $content = get( $url) or die "Couldn't get $url";
my $t = HTML::TreeBuilder->new_from_content( $content );
$t->eof;
$t->elementify;
# Get first match:
my $thing = $t->look_down( _tag => 'p', id => qr/match_this_regex/ );
print $thing ? $thing->as_text : "No match found\n";
# Get all matches:
my #things = $t->look_down( _tag => 'p', id => qr/match_this_regex/ );
print $_ ? $_->as_text : "No match found" for #things;
I'm testing ReST APIs at the moment and found the ReST Client very nice. It's a GUI program, but nonetheless you can save and restore queries as XML files (or let them be generated), embed, write test scripts, and so on. And it's Java based (which is not an ad-hoc advantage, but you mentioned it).
Minus points for recording sessions. The ReST Client is good for stateless "one-shots".
If it doesn't suit your needs, I'd go for the already mentioned Mechanize (or WWW-Mechanize, as it is called at CPAN).
Depending on exactly what you're doing the easiest solution looks to be bash + curl.
The man page for the latter is available here:
http://curl.haxx.se/docs/manpage.html
You can do posts as well as gets, HTTPS, show headers, work with cookies, basic and digest HTTP authentication, tunnel through all sorts of proxies, including NTLM on *nix amongst other things.
curl is also available as shared library with C and PHP support.
HTH
C.
Python urllib may be what you're looking for.
Alternatively powershell exposes the full .NET http library in a scripting environment.
Twill is pretty good and made for testing. It can be used as script, in an interactive session or within a Python program.
Perl and WWW::Mechanize can make web scraping etc simple and easy, including easy handling of forms (let's say you want to go to a login page, fill in a username and password and submit the form, handling cookies / hidden session identifiers just as a browser would...)
Similarly, finding or extracting links from the fetched page is trivial.
If you need to parse stuff out of the resulting pages that WWW::Mechanize can't easily help with, then feed the result to HTML::TreeBuilder to make parsing easy.
What about using PHP+Curl, or just bash?
Some ruby libraries:
httparty: really interesting, the philosophy is interesting.
mechanize: classic good-quality web automatization library.
scrubYt: puzzling at first glance but fun to use.

Categories

Resources