[Tutor] Best way to write this countdown code

Alan Gauld alan.gauld at yahoo.co.uk
Sat Jan 12 12:47:46 EST 2019


On 12/01/2019 15:44, Joseph Gulizia wrote:
> I want to integrate the following working code into a website:

First thing to note is that web sites speak HTML, so anything
you output for display needs to be in HTML not plain text.
(Plain text will usually be understood by a browser but
will look terrible!)

The normal way to build a web app is to create an HTML template
file with place holders for the data. Then use Python (or any
other server language) to inject the data via some kind of
web framework. (Flask and Bottle are two simple Python examples)

An alternative technique involves writing the app in
JavaScript that executes on the browser. That means creating
an HTML file that either contains your app as embedded
JavaScript or putting your JavaScript code into a separate
file and importing it into the HTML. The latter approach
is preferred for industrial sites but the single file approach
is OK for small applications.

Looking at your code we can remove all the lines that
should be in your HTML file:

> beef_quantity = 28  ## Set before campaign starts.
  beef_choice = ""
> if beef_choice == 'Quarter_Beef':
>     new_beef_quantity = beef_quantity - 1
> elif beef_choice == 'Half_Beef':
>     new_beef_quantity = beef_quantity - 2
> else:
>     new_beef_quantity = beef_quantity - 4

The setting of the data values into the HTML will
depend on your framework.

> My end goal (which I still have to figure out) is to have the customer
> click on three sections of radio buttons and enter two text fields
> which will then display in an alert box where they can click a button

That's all HTML function not Python or even JavaScript

> to send them to PayPal for payment.  

Sending to Paypal is a whole new ball game.
That requires quite a bit of extra work.

> the number of available beef will countdown as each order is placed
> until zero s available which will then display "Sold Out!"  Not sure
> if this is ALL python or part python and part javascript.

It could be either or both.
But given your need to send to Paypal I'd suggest you find
a framework and work in Python. (Especially since you seem
to know some Python)

You probably should read the section in my tutorial(or any other)
that covers web applications. It's in the last section starting
with the topic entitled "Working with the Web" and moving on
to "Using Web Application Frameworks"

HTH
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list