Run a python script in a html based website with python packages - python

I made a python chatbot which I want to implement in my HTML (+ CSS & js) website. I want to call the python file with an HTML button and the python file should return a variable (output string) which I could use to display the user on the HTML website.
I've tried Brython, but I couldn't figure out how to import packages
like nltk and tensorflow.
I've tried flask, which works great, but the python file is the main
file, who renders the HTML template. But that's not possible in my
case. The HTML file should be the main file. And the website should remain
on the same URL and, as far as I know (please correct me if I'm wrong),
flask is URL based.
I've tried translating all the python code into javascript manually,
which worked but isn't a long-term solution for me.
So: what is the best way to run a python file with packages externally or internally in a HTML website without redirection?
Thanks a lot in advance!

As you mentioned that HTML file is the main file you are loading the application using some server like nginx or apache? If yes, then why Flask is the problem? It seems that you could load data from Flask app using JS. You would still be on the same URL the JS would do everything in the background and possibly using JQuery you could render html components with data from Flask. (HTML based site loads content from Flask App using JS, the flask app is hosted somewhere else)
However as it is a chat application I would suggest something based on websockets.
The additional option could be to use webassembly for Python which is weird but theoretically possible.

Related

Interact with local HTML file with python

I want to make a script in python that interacts with a webpage that has quite a lot of javascript in it (it's a webpage that computes a bunch of physics stuff).
I don't want my code to break if the page formatting changes and I want it to run offline so I would prefer my script to run on a local html copy of the page I got (all the JS code is accessible in the HTML source, there is no call to an external server). I wanted to use the requests library to do it, but it only works with URLs. Is there any library to do this? Note that I want to interact with the HTML (input values and look at the outputs etc..), I know that I can parse the file but that's not what I'm asking. I'm also totally new to web bots or anything related.
Right now I can open my .html version of the page offline with chrome and interact with it, so there has to be a way to automate this somehow. I'm also not against using something else than python if there is a better library for this in another language.
interesting question, best way I can think to do that is use a web framework and then just scrape the data using requests. I am familiar with flask and its simple to use but im sure there are other options as well

Firefox seems to beautify HTML?

I am trying to make a small crawler app.
So I am constantly looking into the code of page inside of Firefox and I download the same page using urllib inside of python.
Sample URL: https://store.steampowered.com/search/?term=arma 3
And I checked using two separate libraries of python. Same result. File saved by python app looks different when saved on PC from one saved with browser.
Is it browser making the code more readable or server treats differently than standard browser application in a different way?
Thanks!
I tried saving using 2 different python libraries for accessing the web page.
I won't show the code here, because it's 200K of HTML without new lines.
The HTML code is with new lines when downloading page using normal browser.

how to show a django application inside a site in wordpress

I have a site made in wordpress and i´d like to show my django application inside this site. I don´t know how to do that. How do call my django application and show it inside the site.
Your other option - far more work and technical than using an iframe - is to show the content in the WordPress site by using django-rest-framework (on the Django app) then pulling in the content via API requests.
It would be a lot of work, but well worth learning and provide you the most flexibility.
The steps would be
Install django-rest-framework for your Django app
Setup up and configure an API
Either use PHP on the server or JavaScript on the frontend to pull in the content from the API
Only using iframes, because they bore are diffrerent frameworks using different languages etc... so you will make to make one iframe inside your wordpress page and inside that page to load one url from your django application
https://www.w3schools.com/tags/tag_iframe.asp
try to detail your issue. Do you need a link in your wordpress´site ? have you tried loading it using iframe ?
It´s really important to show inside a wordpress´site ?

Run and display results of python script on HTML page

I have written a scraper tool in Python which when executed produces a CSV file of information. I wish to embed it in a HTML, so within the page the user is able to run it and then the results are displayed on the page from the CSV file. How can I do this?
If you only need to display the information on a website, no interaction back with your python script, all you need to do is just write the results into a HTML file and .format() it appropriately.
However, since this requires the user to be able to run and view the results with interaction with your python script, you would need a web server.
You could try the two most popular python based web servers that can accomplish this: Flask and Django or you could use the less common but more lightweight Simple HTTP Server.
But, do keep in mind that an easier alternative (if possible) will be to just use a GUI such as Tkinter or PyQt or EasyGUI.

Integrate Python validation script with HTML

I am planning to build a simple web page that has an upload file option, submit button and a table to display the results.
The file i would be uploading is an XML file.
Upon submit, a python script should validate the XML file and display the discrepancies in the HTML page.
What is the best way to proceed with the front end and python integration? I have already validated the XML using python scripts but i am unable to find out a way to integrate it with HTML, upload & submit buttons.
Kindly help or let me know if you need any further details.
You'll want to use a web framework to do this. Some good options for python are Flask and Django. HTML is meant to run on the client's browser, where Python (in this situation) is meant to run off of a server. Django, Flask, and others make this quick and easy to do.

Categories

Resources