Bottom Page

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)
Larz60+ wrote Sep-08-2019, 02:24 AM:
Please post all code, output and errors (in it's entirety) between their respective tags. I did it for you this time, Here are instructions on how to do it yourself next time.
Quote

Top Page

Messages In This Thread
Flask - Turn console program into a flask program - by MintyScot - Sep-08-2019, 02:21 AM

Possibly Related Threads...
Thread Author Replies Views Last Post
  Running Flask web on other IP than 127.0.0.1 popolon59 1 80 Mar-29-2020, 10:02 AM
Last Post: Larz60+
  Flask - adding new page affects all other pages CMR 15 194 Mar-28-2020, 04:13 PM
Last Post: CMR
  Flask SelectField using Ajax noah_f 0 64 Mar-23-2020, 04:03 PM
Last Post: noah_f
  Flask-Sqlalchemy count products in specific category imawesome 2 137 Mar-12-2020, 08:14 PM
Last Post: imawesome
  Flask - IIS Get Windows User Name beachdogvet 0 110 Feb-28-2020, 12:37 PM
Last Post: beachdogvet
  Flask, Posgresql - Multiple requests are not working bmaganti 5 241 Feb-20-2020, 03:02 PM
Last Post: bmaganti
  flask issues with app structuring sidk23396 1 158 Feb-12-2020, 11:41 PM
Last Post: Larz60+
  Flask-SqlAlchemy - Relationship failed to locate a name - Please Help howardrwb 0 222 Jan-31-2020, 08:38 PM
Last Post: howardrwb
  Simple flask rest api problem cancerboi 4 254 Jan-29-2020, 03:10 PM
Last Post: brighteningeyes
  flask show while calculating keuninkske 0 113 Jan-20-2020, 10:09 PM
Last Post: keuninkske

Forum Jump:


Users browsing this thread: 1 Guest(s)