python script output to webpage - python

I wrote a python script that reads in text from a file and writes a text file of definitions. I want to somehow integrate my program with a webpage for the whole world to see.
I want to be able to retrieve input text from one text box, have the python script process it, then display the output in the other text box.
I have done quite a bit of research thus far but I am still unsure of the best way to go about doing this. I tried using google's app engine but encountered too many problems, for example the app engine runtime environment uses python 2.5.2, I wrote my program using 3.1.2. Other than that I just felt that I was beginning to waste my time trying to port my program over.
I'm starting to think that javascript is the way to go or maybe pyjamas. I was also wondering if it would be possible to just have the python program constantly running on the server and to perform a system call.
I posses very little knowledge when it comes to web development. I appreciate any advice.

You could use the cgi module and create a CGI script, if your server supports it.

It's a much bigger question, involving:
Where are you going to host the site,
How slow is the script (can it execute in a few seconds or not),
Does it need access data from files or a database,
How complex is it,
etc.
I would suggest you read about Django:
http://docs.djangoproject.com/en/1.2/
That is probably the easiest way to set up a simple web site, but is also very powerful if you want to do something more in the future (related to this project or not).
However, since your script is Python 3 only, you don't have too many options, see this question:
https://stackoverflow.com/questions/373945/what-web-development-frameworks-support-python-3
I suppose, if not too hard, it is worth thinking about converting it to Python 2.7.
If that is an option, then you might very well go down to Python 2.5 and use Google App Engine. It gives you many things for free and you really don't need to worry about many things that you would if you were to set up your server. It includes a modified (better to say, shrunk down) version of Django 1.1. When you say you are wasting your time porting from 3.x to 2.5, I guess you were not counting the time that you will waste setting all other things up.

Related

How to run python code directly on a webpage

My problem is as follows:
I have written a python code, and I need to run it on a web page.Basically I need that whatever is on the console should be displayed as it is.
I have no experience in web development and similar libraries, and I need to get this done in a short time. Kindly tell how should I proceed?
Note: I might be plotting some graphs also. It would be great if they could be displayed all at once(sequentially) on the website
https://brython.info/
https://skulpt.org/
https://pyodide.org/en/stable/
There are multiple python implementation on browser, some are webassemble some are javascript.
Is it a good idea to run python on browser as a replacement for javascript in 2022? No it is not, learn javascript. No in-browser python implementation can race with javascript as of today and most probably ever.
You Can't execute Python-Code directly inside a webbrowser - however, you could for instance create a basic IDE in HTML & JS, send code written by a user on the page to a Server, which would then run the code and send the results back to the client-page.
Unfortunately, such a project is quite ambitious and complicated, especially when Security & Stability are of mayor concern, as executing client-code is a very dangerous measure indeed, and requires expertise in Virtualization Techniques & Software.
Another Method could be to use a public API, which allows you to run Python code and fetch the results back. The procedure would be exactly the same as with the previous idea in terms of creating the web-client, but the heavy-lifting - which is actually executing the Python-code, would be taken care of for you.
As you can see, there is no concrete answer to this question, only suggestions.
A few useful links below:
https://docs.docker.com/
https://appdividend.com/2022/01/18/best-python-online-ide/
https://www.makeuseof.com/tag/programmer-browser-ides/
https://www.youtube.com/watch?v=og9Gaj1Hzag
How do I execute a string containing Python code in Python?

Architecture question for app which serves data from SQLite in memory

I am building a dynamic <div> into my (very low-traffic) website, which I want to display only some data from a memory-based SQLite database running within a Python program on the server. Being a novice in web tech, I can't decide which technologies and principles should go into this project.
Right now, the only decided-upon technologies are Python and Apache. Python, at the very least, needs to be constantly running to fetch data from the external source, format it, and enter it into the database. Problem #1 is where this database should reside. Ideally, I would like it in RAM, since the database will update both often and around the clock. Then the question becomes, "How does one retrieve the data?". Note: the query will never change; I want the web page to receive the same JSON structure only with up-to-date values. From here, I see two options with the first, again, being ideal:
1) Perform some simple "hey someone wants the stuff" interaction with the Python program (remember that this program will be running) whenever someone loads the page, which is responded to with the JSON data. This should be fairly easy with WebSockets, but I understand they have fallen out of favor.
2) Have the Python program periodically create/update an HTML file, which the page loads with jQuery. I could do this with my current knowledge, but I find it inelegant and it would be accepting several compromises such as increased disk read/writes and possibly out-of-date data unless read/writes are increased even further, essentially rendering the benefits from a memory database useless.
So, is my ideal case feasible? Can I implement an API into my Python program to listen for requests? Would the request be made with jQuery? Node.js? PHP? Maybe even with Apache? Do I bypass Python by manipulating the VFS? The techs available feel overwhelming and most online resources only detail generating HTML with Python (Django, Flask, etc.).
Thank you!
WSGI is the tech I was looking for!

Hosting Python Script with I/O in <div> on website

My goal is fairly simple, I have written a python 3 script that runs on an ubuntu server at my workplace. Currently I (and other users) have to SSH into this ubuntu box anytime we want to run the script, which with a growing number of users is getting messy - from a convenience and security standpoint. So my boss would like me to create a nice webpage-based GUI to access this script and some other similar scripts I have written.
This seems like a straight forward thing, but the difficulty is in the details - the script is used for viewing and editing records so it has a lot of back-and-forth communication with the user (using input()) as well as screen clears and code like the following:
sys.stdout.write("\033[F") #back to previous line
sys.stdout.write("\033[K") #clear line
...to keep things clean and within one terminal.
If absolutely needed of course I could modify the script to accept inputs in a different format and output in a more html-friendly method without line and screen clears, but this would be rather tedious to redo for the whole script. SO - my question is what would be the best way to achieve what I'm after with minimal modifications to my written code? Ideally the webpage would just have a container holding a terminal-like graphic that handles all the input and output of the selected script as if it was running through a full fat terminal.
Things I've already looked into include basic python web frameworks such as CGI or mod_wsgi, or a python package like flask, but - correct me if I'm wrong - it seems there isn't a nice way to handle constant inputs within scripts with these let alone the terminal-oriented clearing I do for clearing lines and screens.
I also looked into ways to just host a full terminal on the webpage, and security concerns aside, this seems like the closest solution - using something like ajaxterm (which seems rather outdated now?) or wetty. My main concern with this is it isn't any easier than just using SSH at that point. Unless I could modify the terminal to auto-login and start a specific script automagically with the push of a button on the webpage, this would just be a glorified web-rendition of putty.
Any suggestions on how you would achieve this?

How can I call a python's local function from an HTML doc?

I have written a python file with several functions. Now I'm writting a web page where I want to call a function of the file that I mentioned before, by pressing a button but I don't know how.
Can someone help me, or telling me where can I look for some example where python and html are connected.
I'm going to talk only about a front end solution , well known solution for back end only exist (Django) and the others will certainly talk about it.
Beforehand, you should know that using Python as a front end web language is very strange.
The "King" of these languages is JavaScript, and if you want to go further with web development, especially with front end frameworks (AngularJS, react), you should probably learn it.
That being said, there is a way to achieve what you want.
Either with this Python framework, or with a transpiler.
First, you need to set up a server to handle HTTP requests.
Then, something that can interpret python codes is needed. mod_wsgi is a good choice for python. But for a simpler way, you can also use CGI (take a look here).
Edit:
A more complete/structured solution is using a python web framework. For instance, Django or Flask. But be prepared to spend more time on them.

newbie: writing backend code for website

I am usually working in fields of machine learning and hence my background is mostly in stats/ML and no formal web background.
Usually for my project, I work on python which is connected to my local mysql db... to fetch data adn everything.
Now, my work is mostly complete.. everything is console based..
(like traditional programs).
How do I integrate it on the front end. I understand that this is more like a server side scripting.
So, lets take an example of google.
In the front end.. someone enters a search query.. and in the backend lets say there is a program in C++ which executes that query.
How did this interaction takes place.. if front end is written in lets say php..
I assume shell execution of program is a bad bad way to run programs.. ??
Any suggestion will be greatly appreciated.
Thanks
As suggested by Ignacio, you will first need to design API for your project. This is basically clearly laying out what (and how) queries will be supported for your project.
You don't need shell execution and neither need to learn PHP. Since your project is in Python, you can use Python Web-frameworks like Django, Web2Py.
The first thing to do is to develop an API for your library. From there you can develop multiple frontends that use the same API in order to present it multiple ways.
Decouple your user interface logic from your business logic, then reuse the business logic libraries in an application that accepts input over HTTP instead of the console. Django is a popular web framework that will take care of a lot of the front end concerns for you, or you can use something like CGI if you prefer to be a bit closer to the bare metal. The Python Wiki has a section on web programming that you might find useful.

Categories