1

Is it possible to run a python script with HTML & CSS without frameworks like Django and Flask. If so how?

I am trying to run a python script when a button is clicked and get back an output to display onto the screen. How would I do this?

Paul McBurney
  • 231
  • 3
  • 14

2 Answers2

0

Well, Django and Flask do all the HTTP handling for you, so you don't have to do it manually. Althought, as usual in Python, you can do a lot of stuff in one liner way.

You can use SimpleHTTPServer for it.

CHeckout this answer: Single Line Python Webserver

Vitor Villar
  • 1,855
  • 18
  • 35
0

You can use what's called cgi-bin script. Both Apache and NGINX can serve cgi-bin scripts, however NGINX will require an external module to handle this (e.g. FastCGI). Apache has this built in natively (although, I would suspect, also as a module).

Take a look at How to run CGI scripts on Nginx for example configuration of NGINX. This example covers running a Perl script, but this fact is less important. It is, however, relatively complex configuration.

Here's another answer, this time How to run cgi script on apache server, with examples for Apache. Again, the language used is not that important and Python will be just as possible.

The CGI script itself can then be written in any language at all (shell, Python, Perl, C, Go, or anything else you like) - all it has to do is ensure its output is properly formatted, with the first line of output specifying the Content Type, e.g.:

Content-type: text/html

<html>
<head>
    <title>test page generated by CGI script</title>
</head>

<body>
    <h1>boo hoo!</h1>
</body>
</html>

The example Python script generating the above:

#!/usr/bin/env python
#
# Example Python CGI script generating static HTML

HTML = \
"""
<html>
<head>
    <title>test page generated by CGI script</title>
</head>

<body>
    <h1>boo hoo!</h1>
</body>
</html>
"""

def generate_html(html):
    print('Content-type: text/html\r\n')
    print(html)


if __name__ == '__main__':
    generate_html(HTML)

Obviously, this script does nothing useful, but you could have it do something else, and serve the appropriate HTML output.

You then make that python file executable (chmod 755 generate_html.py) and optionally you can rename it to have a .cgi extension (which is how traditionally these files were named; mv generate_html.py generate_html.cgi) and place it in a cgi-bin directory of your web server (I'm assuming here your web server root is /var/www and the cgi-bin directory is /var/www/cgi-bin/ - that's where you'll need to place your generate_html.cgi file).

Finally, your standard index.html (placed in www document root, e.g. /var/www/html) could just be as simple as below:

<html>
<head>
    <title>main page</title>
</head>

<body>
    <p><a href='/cgi-bin/generate_html.cgi'>Do something</a></p>
</body>
</html>

If everything is configured correctly, you should see the output of your Python script (a HTML page with big boo hoo words) instead of the source code of the script itself.

Disclaimer

There are security implications, especially if other users can change the content of the cgi scripts, as these are running on the system, with the same access privileges as the user which the web server application, or the cgi gateway process, is running. Make sure you understand the full implications in your environment.

TL;DR

You can run Python scripts without Flask, Django or any other frameworks as cgi-bin scripts. Both Apache and NGINX support this, but the configuration can be a bit more complex. Not sure how much time you'll save.

mike
  • 293
  • 4
  • 9