Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Python Best ways to run a python program on a web page

njjs1

New Coder
I’ve built a python script that essentially allows you to play a game - you just have to guess the word being defined in each round. The script purely runs in the terminal and I want to create a prettier version of it, like on a website.

I built a quick application using Flask, but I had trouble configuring, for example, a 'start the game' button to execute the script and display the info (word definiton, syonoyms, etc) on the same page. I know JS is usually done for displaying this sort of info (I'm a front-end developer) and I considered using React for the front-end and python for the back-end, BUT I just want to know alternative ways of presenting this on a webpage with just python or something as I'm at the stage of a front-end developer trying to progress towards the back-end.

From my gut feeling (I could easily be wrong) it feels that trying to use python alone to render info that this script uses on the same page doesn't seem like the right approach. So my question is:

What ways could I go about presenting this on a webpage? Using what specific technologies? The research I did, like reading this post, suggested security issues and no solution for even clicking a button to change the content on the page generated from a python script.

Thanks for any advice here. I feel a bit lost regarding how to go about getting the technologies to talk to each other and my question is difficult to explain.

My beginner code can be found here, or below:

Python:
import os
import random
from PyDictionary import PyDictionary

def show_info():
    print("""
Brief: Use the definition and synonyms to guess the word in each round.
Controls:
- enter 'clue' (or 'c') for a clue
- enter 'pass' if you don't know the answer
- enter 'info' for information
""")


print('\n=== Welcome to the Word Guesser ===')
show_info()

DICTIONARY = PyDictionary()
words = []
want_to_play = True
visible_clue = []
correct_results = 0
rounds = 0
undefinable_words = 0
total_attempts = 0
incorrect_attempts = 0
total_clues = 0
used_words = []
cannot_define_words = []

# 1: get list of words (relevant word from each line & add to array)
words = ['esoteric', 'bilateral', 'testing', 'formal']


def show_clue(arr):
    print('Clue: ', ' '.join(arr))


def give_clue(word):
    # 1. generate random letter - change to be one not already in clues array
    letter_no = random.randint(0, len(word)-1)

    # 2. check if that's not already been generated
    while visible_clue[letter_no] == word[letter_no]:
        letter_no = random.randint(0, len(word)-1)
        # continue  # does this do anything
    else:
        visible_clue[letter_no] = word[letter_no]

    show_clue(visible_clue)


def ask_about_continuing():
    global want_to_play
    temp = input("Continue? (Y/N): ").lower()
    while not temp:
        temp = input('You did not enter anything. Continue? (Y/N): ')
    if (temp == 'y'):
        want_to_play = True
    else:
        want_to_play = False


def play():
    global want_to_play
    global visible_clue
    global correct_results
    global rounds
    global undefinable_words
    global total_attempts
    global total_clues
    global incorrect_attempts
    global used_words
    global cannot_define_words
    clues = 0
    attempts = 0

    # 2: generate random word (& visible clue)
    current_word = random.choice(words)
    visible_clue = list(current_word)
    for i, letter in enumerate(visible_clue):
        visible_clue[i] = '_'

    # 3: define & get synonyms
    current_word_definition = DICTIONARY.meaning(current_word)
    current_word_synonyms = DICTIONARY.synonym(current_word)

    if current_word_definition is None or current_word_synonyms is None:
        cannot_define_words.append(current_word)
        print('Could not define word', current_word,
              '. Generating another word...')
        undefinable_words += 1
        return

    used_words.append(current_word)

    # 4: show definition & synonyms
    print('\nDefinition:',
          current_word_definition, '\nSynonyms:', ', '.join(current_word_synonyms))

    # 5: ask for guess or clue
    rounds += 1
    guess = input("Guess the word: ").lower()
    while not guess:
        guess = input('You did not enter anything. Try again: ')

    while guess != current_word.lower():
        if guess == 'clue' or guess == 'c':
            clues += 1
            total_clues += 1
            give_clue(current_word)
            if (clues == len(current_word)):
                print('You did not get this one - the word was', current_word)
                ask_about_continuing()
                break
            # cannot remove, otherwise infinite loop
            guess = input("Now guess the word: ").lower()
            while not guess:
                guess = input('You did not enter anything. Try again: ')
            continue  # needed, otherwise executes next lines even if answer is correct
        if guess == 'pass':
            print('The answer was', current_word)
            ask_about_continuing()
            break
        if guess == 'info':
            show_info()
        attempts += 1
        total_attempts += 1
        incorrect_attempts += 1
        if attempts > 2:
            guess = input(
                "Incorrect. Type 'clue' (or 'c') for help or try again: ")
        else:
            guess = input("Incorrect. Try again: ")
        while not guess:
            guess = input('You did not enter anything. Try again: ')
    else:
        attempts += 1
        total_attempts += 1
        correct_results += 1
        print('Correct in', attempts, 'attempts &', clues, 'clues')
        ask_about_continuing()


while want_to_play:
    play()
else:
    print('\nUndefinable Words:', undefinable_words, ', '.join(cannot_define_words),
          '\nRounds:', rounds,
          '\nCorrect Results:', correct_results,
          '\nAttempts:', total_attempts,
          '\nIncorrect Attempts:', incorrect_attempts,
          '\nTotal Clues:', total_clues,
          '\nUsed Words:', ', '.join(used_words),
          '\nAborting...\n')
 
thank you both. My question was a little vague but I was more enquiring about the interplay between react and python. I think I've figured out (correct me if I'm wrong) that python and react usage together tends to be divided into multiple apps rather than just one - in other words, rather than one app containing both python and react code, building a full-stack app with python and react would require 2 separate applications that communicate with each other via an API. Does this sound right to you?
 

Latest posts

Buy us a coffee!

Back
Top Bottom