Embed Python in an HTML page [duplicate] - python

This question already has answers here:
How can I include python script in a HTML file?
(10 answers)
Closed 6 months ago.
I'm trying to look for a way to embed Python code inside an HTML page.
I'm NOT talking about using Python as the back-end with tools like Django or Flask. I would like to implement a very very basic console on my webpage so that I can show off Python scripts running, not just pure code. The user would then be able to modify the python, then re-run it to see changes.
Suppose I'm making a python programming tutorial website, and I want the user to see that print("hello world"), when run, output "hello world".
Is there a way to achieve this?

Python inside HTML
Allows you to be able to embed Python within HTML documents, similiar to mod_Python or PHP.By taking advantage of both the Module: digiweb and a slightly modified version of PythonInsideHTML.zip from the BSD licensed project Karrigell Python web server to make it a stand alone library. It is possible to run embed Python within a HTML document that can be executed at run time.
Download Here

I found that it's called a "Fiddle". That was the keyword to know for further google searches. I've tried most of them, they don't work, except for one, https://ideone.com/ They also allow to easily embed it in your own web page, which is exactly what I needed.

The best way is to set up a web framework that uses Python. Some good choices are Django, TurboGears, and Pyramid. While it is possible to do pure Python CGI or as an ISAPI extension, you don't really gain much from that approach, and a web framework is bound to be more efficient, understandable, and helpful.
Furthermore, web frameworks come with a philosophy and contain a lot of careful thought and best practices that you can learn from while you're working.
Below are some web site links -
https://docs.pylonsproject.org/en/latest/
http://turbogears.org/
https://www.djangoproject.com/

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?

How can I call a function within a website?

I've been looking at the source HTML for various websites, and it would make my life much easier if I could call functions from the website I'm accessing on will. I'm using Python, but please keep in mind I am somewhat of a novice.
If you're looking to access someone else's website using Python, you'll need to look into something like Python's urllib and beautifulsoup.
If you want to execute functions inside that website (that require Javascript), you'll need to use a Python browser emulator like enter link description here which is described here. Hope this helps! It's quite a complex question.
If you're looking to create a webpage that runs Python code, that's unreasonable, as all Python code in web development is run server-side, and you'll have to rely on something like Javascript for that.

How would I go about uploading a python based file converter/parser to the web?

I have written a python script that takes a text file and reorganizes the information in the file. It takes out certain pieces of information and moves others. So basically it converts it. I want the simplest possible web interface that people can visit. On this interface there is a button to upload a your file then the interface runs this script on it and puts the output file on a new page or the same one for viewing or download. What would be the easiest way to do this? Should I look into Django? I feel like this is so basic that I almost don't need a web framework. I am basically just looking for some good keywords to google for this or other resources. What would this tool be called in web-lingo? Would a framework (django, ect.) help me out that much or is there a way to just basically have an html upload form that calls the python script?
While you could use raw WSGI (the newer way) or Python's cgi module (perhaps more traditional), you might want to look into some minimal web frameworks built on top of WSGI. Here are a few:
Flask
Bottle
CherryPy
web.py
They're all pretty simple to set up and get started with.
Pick one and figure out how to do whatever you need.
Utility Mill makes this kind of utility packaging very easy.

A good Python web framework for existing console program?

I hope I'm asking on the right StackExchange site.
I've written a console program in Python onto which I'd like to put a web interface, but I'm having a hard time deciding what web framework to choose. I don't need much, but I'd like to avoid unnecessary work in trying to use it. I don't have a need for a database (for now), so that's not important to me at all.
I've looked at Django, Web2py, bottle.py, and web.py.
Django and Web2py seem to be great if I were starting out from scratch, but I'm not, and seems a little difficult to integrate into existing code.
bottle.py and web.py almost seem like they could work out, but they're so basic, I'm hoping there's something else out there that wouldn't require so much in the way of templating as these seem to do.
I don't simply want to make a carbon copy of the console interface put into a browser, but rather customize it for a web interface, so I'm not necessarily looking for anything that would simply wrap a console application into a web interface (although that would be interesting too.)
That's a sort of hard problem... Personally I don't see web.py as all that 'basic' as you put it. It should be really easy to wrap your code in some classes with GET and POST functions and be done.
Also, Django can be 'minified' as it were: How do I write a single-file Django application? is a whole conversation about this.
I would say, what is too 'basic' for you? You mentioned 'templating', but how would something magically template for you? There are open source templates for web apps, things like twitter bootstrap come to mind, that kind of give you a ready-made template for your next web app. Also YUI, and dojo do similar sorts of things (tho have a much different focus, since they are full blown JS frameworks).
That said, there is a brand new project called 'shovel' (here): https://github.com/seomoz/shovel
I haven't used it yet, but it seems to do the wrapping of commands into a web interface for you. which you said would be 'interesting'.
Personally I use web.py for all my web stuff.
I suggest Django. I've used Django both for simple mostly static sites and for sites with a lot of forms and I can't say Django imposes any restrictions or forces you to write hundreds LoC even for simple things. Instead you get nice auto generated administrative interface, built-in ORM, internationalization tools and many other things. Thereby, you have great opportunities to grow functionality of your app. In addition it has such vital thing as up-to-date documentation for every module.
Tutorial takes few hours and gives enough information to start developing full-blown sites.
Thanks the continuation which is implemented in the Nagare framework, you can develop a Web application like a console or desktop UI application: put the console code in a component.Task, then create some components for each interaction, i.e. some views that show the data that you print in your console application and receive some user input back. Then, the Nagare framework takes care of the rest: no need to declare URLS, to pass the context from a page to next...
PyQT can be handy if you are looking to implement it, to quote from the RiverBank PyQT website listed below:
"The QtWebKit module implements a web browser engine based on the WebKit open source browser engine used by Apple's Safari. It allows the methods and properties of Python objects to be published and appear as JavaScript objects to scripts embedded in HTML pages."
Source: http://www.riverbankcomputing.co.uk/software/pyqt/intro
Also, do not give up hope if that does not do the trick, as there is also "Pyjamas" which is very handy! Here is a brief description of it:
"Pyjamas is a Rich Internet Application (RIA) Development Platform for both Web and Desktop.
It contains a Python-to-Javascript compiler, an AJAX framework and a Widget Set API. Pyjamas started life as a Python port of Google Web Toolkit, the Java-to-Javascript compiler. Read the FAQ and the list of features."
source: http://pyjs.org
found via: google.com
I would say that when you are always looking to see what the best to use is, ask your question in google, and look on multiple sites and compare the top results of multiple sites to your question, also, it really depends on what you need and what your strong hand plays better in.

python html integration

this is a complete n00b question and i understand i may get voted down for asking this but i am totally confused over python's html integration.
as i understand one way to integrate python with html code is by using mod_python.
now, is there any other way or method that is more effective for using python with html?
please advise me on this as i am new to learning python and could use some help.
some pointers to code samples would be highly appreciated.
thanks a lot.
EDIT: also what i would like to know is, how does PyHP and mod_python compare with regards to each other. i mean how are they different? and Django? what is Django all about?
I would suggest you to start with web.py
You can read a tutorial on how to use Python in the web.
http://docs.python.org/howto/webservers.html
In few words, mod_python keeps python interpreter in memory ready to execute python scripts, which is faster than launching it every time. It doesn't let you integrate python in html like PHP. For this you need to use a special application, like PyHP (http://www.pyhp.org) or another (there are several of them). Read Python tutorial and documentation pages, there's plenty of info and links to many template and html-embedding engines.
Such engines as PyHP require some overhead to run. Without them, your python application must output HTTP response headers and the page as strings. Mod_wsgi and fastcgi facilitate this process. The page I linked in the beginning gives a good overview on that.
Also you may try Tornado, a python web server, if you don't need to stick to Apache.
The standard way for Python web apps to talk to a webserver is WSGI. Also check out WebOb.
http://www.wsgi.org/wsgi/
http://pythonpaste.org/webob/
But for a complete noob I'd start with a complete web-framework (in which case you typically can ignore the links above). Django or Grok are both full-stack framworks that are easy to use and learn. Django is more popular, but Grok is built on 13 years of Web application publishing experience, and is seriously cool. The difference is a matter of taste.
http://django.org/
http://grok.zope.org/
If you want something more minimalistic, the worlds your oyster, there are an infinite amount of web frameworks for Python, from BFG to Turbogears.

Categories