I'm very new to Python. I just know what Python is. I have created the below code (in Python IDLE):
Then I saved this page in my localhost as index.py I run the script using http://localhost/index.py But it does not show the executed Python script. Instead, it showed the above code as HTML. Where is the problem? How can I run a Python file in a web page?
asked Sep 18, 2011 at 10:37
Haren SarmaHaren Sarma 1,9853 gold badges36 silver badges58 bronze badges 8 In order for your code to show, you need several things: Firstly, there needs to be a server that handles HTTP requests. At the moment you are just opening a file with Firefox on your local hard drive. A server like Apache or something similar is required. Secondly, presuming that you now have a server that serves the files, you will also need something that interprets the code as Python code for the server. For Python users the go to solution is nowadays mod_wsgi. But for simpler cases you could stick with CGI (more info here), but if you want to produce web pages easily, you should go with a existing Python web framework like Django. Setting this up can be quite the hassle, so be prepared.
lvc 33.2k9 gold badges69 silver badges96 bronze badges answered Sep 18, 2011 at 10:51
1 As others have pointed out, there are many web frameworks for Python. But, seeing as you are just getting started with Python, a simple CGI script might be more appropriate:
#!/usr/bin/python print('Content-type: text/html\r\n\r') After this the Python code should run just like in terminal, except the output goes to the browser. When you get that working, you can use the cgi module to get data back from the browser. Note: this assumes that your webserver is running Linux. For Windows, answered Sep 18, 2011 at 13:35
jpajpa 9,1971 gold badge24 silver badges41 bronze badges 4 Using the Flask library in Python, you can achieve that. Remember to store your HTML page to a folder named "templates" inside where you are running your Python script. So your folder would look like
This is a small example of your Python script. This simply checks for plagiarism.
This a small template of HTML file that is used:
This is a small little way through which you can achieve a simple task of comparing two strings and which can be easily changed to suit your requirements.
answered Apr 13, 2017 at 6:27
Ash UpadhyayAsh Upadhyay 1,53615 silver badges19 bronze badges 3 If you are using your own computer, install a software called XAMPP (or WAMP either works). This is basically a website server that only runs on your computer. Then, once it is installed, go to the xampp folder and double click the htdocs folder. Now you need to create an HTML file (I'm going to call it runpython.html). (Remember to move the Python file to htdocs as well.) Add in this to your HTML body (and inputs as necessary).
Now, in the Python file, we are basically going to be printing out HTML code.
answered Jun 25, 2015 at 14:36
rassa45rassa45 3,4061 gold badge28 silver badges43 bronze badges 0 Well, the OP didn't say server or client side, so I will just leave this here in case someone like me is looking for client side: Skulpt is a implementation of Python to run at client side. Very interesting, no plugin required, just simple JavaScript code.
answered Jul 9, 2019 at 11:49
MarcoMarco 2,60120 silver badges24 bronze badges 1 With your current requirement, this would work:
Open the index.html file, and you will see what you want.
answered Feb 1, 2019 at 7:40
1 Can you run a python script on a website?The Python language uses CGI to execute on a Web page. You must import the "cgitb" library for the Python language to execute and display the results in a Web browser. The Python language uses small statements that make it suitable for large or small Web projects.
How do I embed a python code on my website?How to Embed a Python Interpreter in Your Website?. Scroll down a bit until you reach the embedded Python interpreter.. Type in the Python code you would like to embed on your website.. Click the menu item </> Embed .. Copy and paste the code <iframe> ... </iframe> into your website.. |