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
  Flask_table module compatibility issue: cannot import name 'Markup' from 'flask' venkateshbalagiri 1 177 Mar-22-2024, 05:07 AM
Last Post: venkateshbalagiri
Shocked FLASK confuse LennyKiz 1 482 Nov-05-2023, 04:23 PM
Last Post: Axel_Erfurt
  Flask - use bootstrap styles in HTML Krayna 1 1,004 Aug-29-2023, 02:33 PM
Last Post: menator01
  Flask CORS not functioning michaelnicol 1 1,313 Jul-02-2023, 05:04 PM
Last Post: michaelnicol
  Flask & Files Gilush 1 1,662 Apr-07-2023, 08:23 AM
Last Post: SpongeB0B
  Flask: sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) database is locked pythonpaul32 1 2,029 Apr-04-2023, 07:44 AM
Last Post: Larz60+
  Flask error sqlalchemy.exc.IntegrityError: (sqlite3.IntegrityError) UNIQUE constraint pythonpaul32 2 3,494 Feb-21-2023, 03:13 AM
Last Post: noisefloor
  flask.cli.NoAppException: Could not import 'app' kazu755 4 4,600 Feb-19-2023, 01:50 PM
Last Post: Larz60+
  Flask and SQLAlchemy question: Database is being created but tables aren't adding pythonpaul32 3 4,414 Feb-07-2023, 10:48 AM
Last Post: pythonpaul32
  REST API using flask - limit connection? korenron 1 1,210 Feb-05-2023, 06:48 PM
Last Post: noisefloor

Forum Jump:

User Panel Messages

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