Python Forum
Flask - Turn console program into a flask program - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Web Scraping & Web Development (https://python-forum.io/forum-13.html)
+--- Thread: Flask - Turn console program into a flask program (/thread-20952.html)



Flask - Turn console program into a flask program - MintyScot - Sep-08-2019

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)



RE: Flask - Turn console program into a flask program - noisefloor - Sep-08-2019

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