I'm trying to make the hello world example work for python via azure functions. The basic function tries to retrieve a name as input through the url and then respond "Hello Name". It turns out that the example-template which is available through the azure portal does not work out of the box. The basic example looks like this:
import os
import json
postreqdata = json.loads(open(os.environ['req']).read())
response = open(os.environ['res'], 'w')
response.write("hello world from "+postreqdata['name'])
response.close()
The two environment variables req and res are paths to temporary files storing input and output from the function as json. The idea is that input passed through the url should be available in the dictionary returned by json.loads(). The only dilemma is that the file located at os.environ['req'] is empty no matter what I do.
os.path.isfile(os.environ['req'])
# Returns True so the file is located at:
# D:\local\Temp\Functions\Binding\79fcec12-baf3-470e-87c3-113f64ffcef0\req
# during the execution
I tried the Hello world JavaScript example as well which works directly out of the box on azure-functions. My python script works fine when executing in within the azure-portal, but fails when triggering it from a web browser.
The function runs on Python 2.7.13 with extension &name=MyName to the https adress.
I believe the bug is not in the script itself, but hidden in the backbone somewhere. Anyone tried the same?
The default sample you refer to (source here) accepts an http POST request, and requires your request body to be a JSON payload with a "name" property.
To access URL query parameters, we make them available to you as distinct environment variables. For example, if you send query parameter "foo", you can access it via os.environ['req_query_foo'].
I was also having this issue.
The example at this link says to test it with a url of type
https://<app_name>.azurewebsites.net/api/MyHttpTrigger?name=<yourname>
When testing locally this format works, but I finally figured out that when testing once deployed that the link it gives in the command line of type
https://<appname>.azurewebsites.net/api/MyHttpTrigger?code=<randomcode>/<randomcode>==
is what you really need. Add on your &name=<yourName> to this link instead. Mine worked for
https://<appname>.azurewebsites.net/api/MyHttpTrigger?code=<randomcode>/<randomcode>==&name=<yourName>
Not sure if you have already tried this since your question wasn't entirely clear, but hope this helps.
Related
So I had a number of amino acid sequence strings that I wanted to use as input into a tool that studies its interactions with certain components of the human immune system (http://www.cbs.dtu.dk/services/NetMHCcons/).
I wanted to ask what, if any, would be a way of accessing, inputting data and getting the output, via a script (R or python preferably). My main issue was I had a lot of sequences that need to be queried separately so wanted to automate the whole thing. The website has one field that reads "Submission" which takes in the string input. There is another field "select species/loci" which gives a drop down menu from which an option needs to be selected. Lastly there's a "submit" button. The output simply loads on the page after hitting submit.
I've tentatively poked around with RSelenium and Rcurl but wanted to ask if there was a more efficient method.
I took a look at what it'd take to send a POST request to this service from Python, and it looks possible:
this form takes in "multipart/form-data" (see: How to send a "multipart/form-data" with requests in python?), you'll need to send your data in this format. You could inspect a request from the browser (using the dev tools) and copy the fields from there as a starting point.
once the form is submitted, it doesn't give you the result right away. You'd need to get your job ID from the response, and then poll the URL: http://www.cbs.dtu.dk/cgi-bin/webface2.fcgi?jobid={your_job_id}&wait=20 until it gives you the result
the result will then need to be downloaded and parsed
This tool is however available as a portable version for linux/mac: https://services.healthtech.dtu.dk/software.php
Perhaps downloading this version would make it easier?
Try this :
Submitting to a web form using python
This link is an answer to how to send web forms in python, using urllib. Check your source code and extract the necessary data using re module from the source code of the link you have put up, and send the request.
save the HTML source code of http://www.cbs.dtu.dk/services/NetMHCcons/ in the python file as
source_code = '''...'''
The HTML can be found by using CTRL+U in firefox.
I use a webapp that can generate a PDF report of some data stored in the app. To get to that report, however, requires several clicks and monkeying around with the app.
I support a group of users of this app (we use the app, we don't create the app) and I'd like them to be able to generate and view this report with as few clicks as possible. Thankfully, this web app provides a lot of data via a RESTful API. So I did some scripting.
I have a Python script that makes an HTTP GET request, processes the JSON results, and uses that resultant data to dynamically build a URL. Here's a simplified version of my python code:
#!/usr/bin/env python
import requests
app_id="12345"
secret="67890"
api_url='https://api.webapp.example/some_endpoint'
resp = requests.get(api_url, auth=(app_id,secret))
json_data = resp.json()
# Simplification of the data processing I'm doing
my_data = json_data['attr1']['attr2'] + my_data_processing
# Result of the script is a link to a dynamically generated PDF
pdf_url = 'https://pdf.webapp.example/items/' + my_data
The above is a simplification of the code I actually have, but it shows the relevant points. In my actual script, I continue on by doing another GET with the dynamically built URL. The webapp generates a PDF based on the my_data portion of the URL, and I write that PDF to file. This works very well today.
Currently, this is a python script that runs on my local machine on-demand. However, I'd like to host this somewhere on the web so that when a user hits a URL in their browser it runs and generates the pdf_url, instead of having to install this script on each user's local machine, and so that the PDF can be generated and viewed on a mobile device.
The thought is that the user can open http://example.com/report-shortcut, the python script would run server-side, dynamically build the URL, and redirect the user to that URL, which would then show the PDF in the browser (assuming the user is using a browser that shows PDFs like Chrome, Safari, etc). Alternately, if a redirect is problematic, going to http://example.com/report-shortcut could just show an HTML page with a link to the URL generated by the Python script.
I'm looking for a solution on how to host this Python script and have it run when a user accesses a webpage. I've looked into AWS Lambda and Django, but both seem like overkill for such a simple script (~20 lines of code, plus comments and whitespace). I've also looked at Python CGI scripting, which looks promising, but I have no experience setting up something like that.
Looking for suggestions on how best to host and run this code when a user goes to the example URL.
PS: I thought about just re-implementing in Javascript, but I'd rather the API key not be publicly accessible.
I suggest building the script in AWS Lambda and using the API Gateway to invoke it.
You could create the pdf, store it in S3 and generate a pre-signed URL. Then return a response 302 to the user to redirect them to the pre-signed URL. This will display the PDF in their browser.
Very quick to setup and using Boto3 getting the PDF into S3 and generating the URL is simple.
It will be much simpler than some of your other suggestions.
See API Gateway
& Boto3
I'm developing my Django website since about 2 months and I begin to get a good global result with my own functions.
But, now I have to start a very hard part (to my mind) and I need some advices, ideas before to do that.
My Django website creates some PDF files from HTML templates with Django variables. Up to now, I'm saving PDF files directly on my Desktop (in a specific folder) but it's completely unsecured.
So, I installed another web application which is named LogicalDoc in order to save PDF file directly on this application. PDF files are created and sent to LogicalDoc.
LogicalDoc owns 2 API : SOAP and REST (http://wiki.logicaldoc.com/rest/#/) and I know that Django could communicate with REST method.
I'm reading this part of Django documentation too in order to understand How I can process : https://docs.djangoproject.com/en/dev/topics/http/file-uploads/
I made a scheme in order to understand what I'm exposing :
Then, I write a script which makes some things :
When the PDF file is created, I create a folder inside LogicalDoc which takes for example the following name : lastname_firstname_birthday
Two possibilities : If the folder exists,I don't create a new folder, else I create it.
Once it's done, I send the PDF file directly inside the folder by comparing PDF name with folder name to do that
I have some questions about this process :
Firstly, is it possible to make this kind of things ?
Is it hard to do that ?
What kind of advices could you give me ?
Thank you so much !
PS : If you need some part of my script, mainly PDF creating part, I can post it just after my question ;)
An idea is pretty simple, however it always requires some practice.
I strongly advice you to use REST api and forget about SOAP as the only thing it can bring to you - is 'pain' :)
If we check documentation, document/create it gives next information.
Endpoint we have to communicate with.
[protocol]://[server]:[port]/document/create
HTTP method to use - POST
List of parameters to provide with your request: body,
document, content
Even more, you can test API by clicking on "Try it out" button and check requests in "Network" tab of your browser (if you open Developer Tools)
I am not sure what kind of metadata do you have to provide in 'document' parameter but what I know you can easy get an idea of what should be done by testing it and putting XML or JSON data into 'document' parameter.
Content is an array of bytes transferred to the server (which would be your file).
To sum up, a request to 'document/create' uri will be simple
body = { 'headers': {},'object': {},}
document = "<note>data</note>"
content=open('report.xls', 'rb') #r - reading, b - binary
r = requests.post('http://logicaldoc/document/create', body=body, document=document, content=content)
Please keep in mind that file transferring requests take time and sometimes you may get timeout exception. Your code will stop and will be waiting for response, so it may be a good idea to get some practice with asyncio or celery. Just keep in mind those kind of possible issues.
So I am attempting to use bottle.py and twitter bootstrap together to make a small website. I need to be able to insert a reasonable amount of data at various points in the HTML using Python but I am not really sure I understand how the HTML and python are communicating.
Here is an example of twitter and bottle working together.
He mentions linking a couple of .js files in the html and I can see where he does that but I am not really sure how that affects how the python interacts with the html. Is there a callback from the Javascript that the python catches using request.GET.get().strip():?
Also one of the last lines is:
return template('templates/gpio.tpl', colour1=colour1, colour2=colour2, colour3=colour3)
I am not sure how the templates/gpio.tpl is connected to the html he mentions below. I understand that the colour# variables are referenced in the html (I assume this happens with the {{}} syntax) but I am not sure how the html gets called at all.
From what I understand (which so far isnt a whole lot) this is how it goes:
User enters "server:port/gpio" into a webbrowser
The python decorator gets called by bottle and the function is run returning the template at the bottom.
This is where I get confused.
A) How does the python script know to call the html?
B) How does the gpio.tpl template code get sent to the html?
C) Is it safe to assume that the python arguements sent to the template function can be referenced using the {{}} syntax or is there more to it?
D) How does the html call back to the python to update the buttons he shows at the bottom?
D.1) does the JS linked at the top have something to do with this?
Lastly: If anyone has a another/better example of linking bootstrap and bottle I would be very happy to see it.
This is quite a loaded post. Thank you for your patience. :D
You'd really need to first learn how the HTTP protocol works... But let's try to quickly answer your main question:
how the HTML and python are communicating
Quite simply: they don't. What happens is:
your client (usually your browser) send an HTTP request to your site
the front web server (Apache, Nginx, whatever) sends this request to the bottle.py application
the bottle.py app dispatch the request to the right controller function matching the url's path portion and request's method against the defined routes)
the controller function does what it has to do and returns an HTTP response to the front web server
the front web server send this response to your client
Usually - but not necessarily - the HTTP response contains HTML content, generated by the controller using a template. IOW : bottle.py uses the template to generate html that is sent back to the client.
Once the response is sent, there's no more "communication" until the client sends another request.
So if I wanted to make an html button that changed something on the page how would I send that response back to bottle.py regenerate the page with the change?
It depends on what you want to change...
For example, pushing the button could trigger a new HTTP request, you should then define a new feature in your code.
Let's take the previous example, and imagine you want to add a switchOff button.
You have to add the following button somewhere in the gpio.tpl :
<input type="submit" class="btn" name="LedsOff" value="Turn off the leds!">
Then, modify the function gpio() to add a new condition with the following :
elif request.GET.get('LedsOff','').strip():
from quick2wire.gpio import Pin, exported
with exported(Pin(12, Pin.Out)) as out1, exported(Pin(13, Pin.Out)) as out2:
out1.value = 0
out2.value = 0
following the proper auth from the standard api calls .
after making the complete authentication
shortname= shopify.Shop.site
shortname=shortname[8:-20]
url='http://mysite.com/widget/v1/'+str(shortname)+'/script.js'
shopify.ScriptTag(dict(event='onload', src=url)).save()
this give the response as
ipdb> shopify.ScriptTag(dict(event='onload', src=url)).save()
True
which i suppose now creates the script tag in store site
but when opening the store online i am not able to find my script url in the store's source code .
any idea why things are not woking that way ??
The script is injected into the code when the onload event is fired, it's not directly inserted into the code before. If you're looking at the source of your store you should see your scripts url inserted into the asyncLoad function. This function just loads all script tag js files when onload is fired. If it's still not there, you should try getting the list of script tags through the API and make sure yours is in that list.