How to run simple Python script from web server?

Soldato
Joined
18 Oct 2002
Posts
10,948
Location
Bristol
Little project with my kid, she wanted 'fairer' dice for a board game, to remove some of the element of luck.
We wrote this little script, which basically guarantees the expected distribution.

My question is how to make this run on a website? I've never done anything with Python online. Is there a simple way to get this script working on a webpage (phone)? I already have decent web server / host.

Python:
import random

outcomes = [2, 3, 3,  4, 4, 4, 5, 5, 5, 5,  6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 11, 11, 12]

def roll_dice(outcomes):
    if outcomes:
        roll = random.choice(outcomes)  # Choose a random outcome
        outcomes.remove(roll)  # Remove the chosen roll from the list
        return roll
    else:
        print("Dice reset")
        outcomes.extend([2, 3, 3,  4, 4, 4, 5, 5, 5, 5,  6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 11, 11, 12])
        return roll_dice(outcomes)


print("Welcome to FAIR DICE!")

input_string=""
while input_string != "q":
    input_string=(input("Press Enter to roll [q - Quit]..."))
    print("Result =",roll_dice(outcomes))
 
Associate
Joined
26 Dec 2008
Posts
1,020
Location
Cornwall
Running a python script on a third party web host is likely to be quite difficult without quite a few hoops - what is your hosting setup?

I would expect it'd be easier to reimplement this in HTML + JavaScript to have a nice web frontend for it
 
Last edited:
Soldato
Joined
4 Nov 2006
Posts
2,918
Location
London
As above, if running it via the browser (i.e. serving it as HTTP) it'll need a customised or specific host that allows running of python files (via CGI or some sort of web server extension).

Also it would potentially need to be reworked maybe into an API (see FastAPI) as you can't really do input() on a webpage running a python file.

Maybe you mean running it in an online REPL (Read Eval Print Loop) like the local version that comes with Python (the command line program), if that's the case there are many online code environments you can run the program in. There might be a PyFiddle site built to host user created code.
 
Back
Top Bottom