Python Forum
Flask - Turn console program into a flask program
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Flask - Turn console program into a flask program
#1
Hi Everyone!

I've been learning to code in my spare time, so still a bit of a noob.

I want to make a flask GUI for this console program I've written.

Nothing too fancy, just a vending machine. Enter 1 to buy chips, enter 2 to buy chocolate etc.

I thought I could just kind of bolt flask onto it :(

Basically I want a webpage where you click a button to choose a snack and Flask does code after that.



A quick overview of my console program:

def Welcome():
	# Displays a text console menu. 
	# Press 1 for this item, press 2 for that item
	
	
def User_Input():
	# gets user console imput
	
	return user_selection

	
def Process_Stock(user_selection, stock_level, payment_token, sales_total):
	# Checks the stock level for the item the user selected.
	# If the stock level (saved in stock_level list) is > 0 a message is displayed and stock count -1
	# A payment_token is set to the user selection and passed to Payment() and Dispense() if in stock.
	
	
def Payment(payment_token, stock_level, sales_total):
	# The payment_token is used to decide how much to charge.
	# If payment_token = 1, then ask for the price of item 1...

	
def Dispense(payment_token):
	# Uses a switch to dispense whatever item payment_token is set to.
	
	
def Main(stock_level, sales_total):

	user_selection = 0  # Variable holds the key the user pressed, 1-8, 0 if unused/error
	payment_token = 0  # Token is set to the key/item number of user selection (if in stock) and passed to payment function

	while(user_selection == 0):

		Welcome()

		user_selection = User_Input()

		# Process Stock
		# Check the stock level
		# Add one to the sales_total if in stock
		# reduce stock level by 1
		# set payment_token to the key number.
		payment_token = Process_Stock(user_selection, stock_level, payment_token, sales_total)
		
		# If stock present, ask for payment
		# Payment_token is set to the item selection key number if there's stock.
		Payment(payment_token, stock_level, sales_total)

		# Dispense item
		Dispense(payment_token)



	return stock_level, sales_total
	

if __name__ == '__main__':
	stock_level = {0: 0, 1: 5, 2: 5, 3: 5, 4: 5, 5: 5, 6: 5}  # Selection number : items in stock
	sales_total = {0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0}  # Selection number : sales total for that item

	Main(stock_level, sales_total)
Where do I start writing the flask routes, that call functions?
Would I have to re-write the whole thing and make each button call the functions individually or can I still use main()?

	
@app.route("/")
def index():
	return render_template('index.html')

# Press Key 1
@app.route("/key_one", methods=['POST'])
def button_1():
	item = 'Chips - Cheese 50g'
	price = 1
	return render_template('confirmation.html', title="Pressed Key 1!", item = item, price = price)
Reply
#2
Hi,

Quote: Basically I want a webpage where you click a button to choose a snack and Flask does code after that.
You need to look into HTML forms and working with form data on the server side (=Flask). Do you have a base knowledge of HTML?

Regards, noisefloor
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Using Flask with thonny for my project website need help Confusednoob 3 1,940 Mar-16-2025, 05:42 PM
Last Post: snippsat
  Moving flask app to sub folder on live site. Help This is driving me crazy **huh** Rosssaab 1 1,006 Jan-10-2025, 02:11 PM
Last Post: menator01
Question running Flask with waitress having web traffic log... SpongeB0B 3 8,160 Dec-09-2024, 03:34 AM
Last Post: bazinga
  Unable to Generate Class Code in Flask App - Form Not Submitting Correctly misbahskuy 1 1,274 Dec-08-2024, 07:07 PM
Last Post: snippsat
  Flask web app MySQL runtime error TheTiger 8 6,328 Dec-04-2024, 03:18 AM
Last Post: TheTiger
  Docker Flask App on windows system Osaci 0 1,043 Oct-27-2024, 06:12 PM
Last Post: Osaci
  Web app with DB Crud can't pip install flask-mysqldb TheTiger 4 2,538 Oct-23-2024, 12:30 AM
Last Post: TheTiger
  How do I capture URL information with Flask? MacAarthur 0 1,012 Sep-16-2024, 06:37 AM
Last Post: MacAarthur
  Flask_table module compatibility issue: cannot import name 'Markup' from 'flask' venkateshbalagiri 3 3,026 Sep-13-2024, 06:14 AM
Last Post: buran
  Flask basic help needed aArtur 2 997 Sep-09-2024, 06:45 PM
Last Post: aArtur

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020