Python in HTML

Started by Dexterous, November 11, 2017, 07:42:26 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Dexterous

Hi, I've got a bit of Python code I'd quite like to embed in a webpage or convert to html, anyone know of a plugin etc. that might help or will I need to convert it manually?
"This team composition is draining my will to live" - Fellow Overwatch Players

Galatoni

We're going to need some more information. What does the app do? Is it solely for use on a webpage?

The general idea is you provide a framework that'll handle the request (Django is a popular one for Python) and then have the endpoint (your application code embedded in the framework) provide the data back to the end user. The data is then displayed as part of a webpage. But depending what you actually want to do, you might need more than just the simple stuff. This all comes down to what you want your app to do, who it's for, and how you want it displaying?

I'm a PHP man myself. But I'm always up for learning new stuff though, so i'll help if I can.
"Forewarned is forearmed"

Dexterous

Right now I'm just playing around with the language and am just looking to see how I would go about integrating any code I wrote into a webpage. The app that I'm using as an example wouldn't be doing anything massively complex, the idea is that it will generate random numbers until a number below or equal to 3 comes up.

import random
import time

chance = 3
counter = 1
result = 0
def Example():
    global counter
    global result
    global chance

    result = random.randint(1,100)
       
    if result<=chance:
        print("CRASH! Game over")
        quit
    else:
        print(counter)
        counter = counter+1
        time.sleep(0.1)
        Example()

Example()


As shown in the code above (and yes I know it's horribly written) it's basically just a counter with a 0.03 probability of stopping every tick.
The best way this could be displayed would be by having a counter on the webpage (refreshing every 0.1 seconds)
As I said, it wouldn't be doing anything particularly difficult but ideally I'd like to nail down the core ideas behind using python in webpages, otherwise I'll probably just go down the route of doing this sort of thing in html.

By the sound of your post though I would need a definitive end point with a chunk of data that would be displayed all at once rather than having it update to show the new data periodically.
"This team composition is draining my will to live" - Fellow Overwatch Players

Galatoni

Well. If you're not attached to your language, something like this could be easily deployed in JavaScript. But there are plenty of 'getting started' guides that could help with using Django.
"Forewarned is forearmed"

Twyst

Quote from: Galatoni;427971I'm a PHP man myself.

PHP has come a long way, but it has a very checkered security history.

QuoteBut I'm always up for learning new stuff though, so i'll help if I can.

As am I! I'm currently teaching myself NodeJS and VueJS so I can do a back/front end in the same language.
I did play around with GoLang, but something about it turns me off.

Galatoni

Quote from: Twyst;428023PHP has come a long way, but it has a very checkered security history.

You're not wrong. But its important to note that this is largely due to misuse, not the language itself.
"Forewarned is forearmed"