I have created a button in HTML , i want to invoke a external python script when that button is clicked.
and also pass on some variables into that python script if possible.
which method would be the simplest and fastest.
TIA
I tried flask but that didnt help me, my html code is being hosted on apache. i cannot / shouldn't (maybe) use flask
If you are not using Flask, you can use Django. If you don't want to use Django either, you can use ajax to call the script as Fastnlight has shown above.
In case that does not work, you can use "py-script". It is like javascript. It is in experimental phase but can do simple tasks in your browser window.
link to pyscript: https://pyscript.net/
Related
I tried to link my HTML file with Python code.
I tried this
import webbrowser
webbrowser.open_new_tab("data.HTML")
It returned my HTML page in Firefox..
But i need to return to my Python program to execute remaining lines
But when I closed this browser, it closes my Python script too.
And I tried to link my Python program by,
go to Python
it returns to text editor not to terminal...
But I need to return to terminal.
I need the solution
As someone described, you need to use one web framework (like flask, django others) to run python code. Or the second solution is using CGI(http://modwsgi.readthedocs.io/en/develop/).
For the second problem(want to keep running python code after browser is closed), I want to advice to use Selenium.
Cheers, John
I have written an application in python to collect data from a javascript form and returned the processed text. It is based entirely off of the code here (but a lot more complex, so I have to use python for this).
https://kooneiform.wordpress.com/2010/02/28/python-and-ajax-for-beginners-with-webpy-and-jquery/
(note to people who like to edit...please leave this link in place since it shows all the relevant code sections in python and javascript).
I need to use this in wordpress (since that's what runs my site) and I honestly have no idea how to pull this off. Webpy can run with Apache CGI, but the documentation (http://webpy.org/cookbook/cgi-apache) is only clear if one wants to navigate directly to the python app as its own page.
I'm hoping someone here has expertise in how to embed this all within a Wordpress page/post?
Thanks!!
As far as I know, there is no native way to run Python code inside a WordPress site just like php. In fact, if you are not doing anything unique to Python, I would suggest you to use php, which supports regular expression and can be used in WordPress by installing the plugin "Insert PHP".
If you really want to use Python, then you need an API endpoint where you connect the function to your website. You would have to look into Azure Function App/AWS lambda on which you write a function app to work as a backend. Then whenever someone request your website, your website would do an HTTP request to that API.
Can you explain what exactly you want to do on your website?
I have 10 line python code and a third party python library that I want to use in a html website. I do not want to use a full fledged framework like django to complete the task. I want to use the following library https://github.com/nficano/pytube in my project. Thank You looking forward for some assistance.
The most simple thing come to my mind is following.
1) You'll need to use some sort of WSGI (https://uwsgi-docs.readthedocs.io/en/latest/, for example), to serve your script as a web service.
2) You'll also need a little bit of JavaScript code to make AJAX (for example) requests to that script.
I am a python beginner and I need some help to create a web service that calls a python web scraping script (a task for a course).
I can use Bottle to create the web service. I wanted to use static_file to call the script but I am not sure about that because the documentation says that static_file is for CSS.
The idea is first to create the web service and later used the web scraping script from a server.
Thanks for your help and greetings from Colombia!
P.S. I don't have an excellent English but I hope someone can understand me and help me.
unless it's already in a function, edit your scraping script so your code is contained within a function that returns whatever information you want. it should be as easy as indenting everything unindented and adding a def main():
let's say your script is called scraper.py and it's located along with your bottle controllers, at the top of your controller file add a import scraper.
in your callback you can call scraper.main()
(not sure why pasting the code here is not formatting it's below)
Having that said, it's usually bad practice to have something long running like a scraping script in a request. You'd usually want to use a queue of scraping jobs where your controller posts work to do, your scraper subscibes to it and notifies it when it's done caching the results somewhere.
from bottle import route, run
import scraper
#route('/scrape')
def scrape():
return scraper.main()
you could try this guide I found:
http://docs.python-guide.org/en/latest/scenarios/scrape/
For the xpath stuff, I would suggest using Mozilla Firefox with the "Firebug" pluggin. It can generate xpaths for you which will help you write your script faster
I want to execute a python programme whenever the user clicks on any of the buttons from the django submit_line.html page.
I have tried using variations of the following:
onclick="python script.py"
onclick="/usr/bin/python /home/django/project/script.py"
onclick="python ../../script.py"
but I can't get the script to run.
Does anyone know of any solutions to my problem?
Thanks for your time.
If instantly: use javascript.
If by refreshing the page: just use requests and write your Python code as function in views.py
You don't seem to have understood anything from the Django tutorial or documentation.
In Django, you access the server-side functionality via URLs, which are served by views. You don't reference a Python script directly in your template, you reference a URL which maps to a view, and your Python code goes in that view.
Quite apart from that, onclick is Javascript. You can't just reference a Python script, that makes no sense. You can write a Javascript function to call (via Ajax) a URL which does what you want, or more simply make it a link to that URL.