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
#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
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Restful API Python Flask Postman starter_student 0 28 Today, 07:12 AM
Last Post: starter_student
  Access Flask Web App from External Browser - IIS fioranosnake 3 87 Sep-16-2019, 06:22 PM
Last Post: ndc85430
  Google calendar pthon flask auth Kireta 0 70 Sep-16-2019, 04:50 PM
Last Post: Kireta
  Using flask to add data to sqlite3 table with PRIMARY KEY catafest 1 360 Sep-09-2019, 07:00 AM
Last Post: buran
  got some problem with flask signup form. darktitan 1 154 Aug-30-2019, 06:05 AM
Last Post: aligoren
  Flask: Making my first web site Truman 37 1,977 Aug-22-2019, 02:02 PM
Last Post: snippsat
  wbserver roundSlider widget doesn't start/update audio tone program Hadad 8 1,332 Aug-01-2019, 05:46 PM
Last Post: Hadad
  How to create Flask-Stripe Checkout and Charge for the multiple items Antares 4 354 Jul-05-2019, 10:20 AM
Last Post: Antares
  [Flask] How to paginate a list of posts SheeppOSU 2 240 Jun-22-2019, 07:45 PM
Last Post: SheeppOSU
  Flask data with dynamic updates from panadas parthi1705 0 181 Jun-19-2019, 09:59 AM
Last Post: parthi1705

Forum Jump:


Users browsing this thread: 1 Guest(s)