简体   繁体   English

Looking for a way to get input from a html form stored as python variable and after some calculation is doen, print the output too in a html page

[英]Looking for a way to get input from a html form stored as python variable and after some calculation is doen, print the output too in a html page

So I kinda know HTML but suck at python.所以我有点知道 HTML 但对 python 很烂。 As of now, I used Django(didn't understand) a web scraping module called beautiful soup(which didn't work).截至目前,我使用了 Django(不明白)一个 web 抓取模块,称为美丽汤(没用)。 Basically what I am making is a billing software with item codes for items and a few varieties of quality and various costings per unit price.基本上我正在制作的是一个计费软件,其中包含项目的项目代码以及几种质量和每单位价格的各种成本计算。 I was able to figure out how to get the output part of the code working by using txtfile.write(HTML) .我能够弄清楚如何使用txtfile.write(HTML)使代码的 output 部分工作。 I want my output as a table so I am specifically using pandas and also requesting for the answer output to have pandas.我想要我的 output 作为一个表,所以我专门使用 pandas 并且还请求 output 有 Z25DFA405AZC4FEFA822 的答案。 NOTE: Can we do this without involving javascript at all?注意:我们可以在不涉及 javascript 的情况下做到这一点吗? I don't want to use javascript because我不想使用 javascript 因为

  1. I didn't learn it我没学过
  2. I want to use only 1 programming language type thing我只想使用 1 种编程语言类型的东西

As of now, I have 3 problems:截至目前,我有3个问题:

  • How to get input from HTML form and store it as a python variable如何从 HTML 表单获取输入并将其存储为 python 变量
  • How to format the output HTML part如何格式化 output HTML 部分
  • How to ensure that if I wanted to add more items, the python for loop will also be applicable to both the python calculation part and HTML part如何确保如果我想添加更多项目,python for 循环也将适用于 python 计算部分和 HTML 部分

I can work with either Tkinter or flask, flask is highly preferable.我可以使用 Tkinter 或 flask,flask 是非常可取的。

This is the python code part这是 python 代码部分

import pandas as p
from IPython.display import display
from tabulate import tabulate
import webbrowser
import os
import cgitb
cgitb.enable()

shop_bill = {'101A': ['Brown Rice', 50, 45.50, 41.25], '102B': ['Whole Wheat', 30, 27.45, 21.50],
             '102C': ['Tomato Sauce', 25.50, 20.25, 18.70], '103D': ['Mustard', 40, 39.45, 37],
             '104E': ['Barbeque Sauce',
                      45, 43, 41.50], '105F': ['Red Wine Vinegar', 4000, 3800, 3750], '106G': ['Salsa', 200, 180, 170],
             '107H':
                 ['Extra Virgin Olive Oil', 500, 478.50, 455.70], '108J': ['Canola Oil', 200, 180, 118],
             '109J': ['Hot Pepper Sauce', 100, 98.50, 91.25],
             '110K': ['Bananas', 60, 55, 50], '111L': ['Apples', 300, 250, 120], '112M': ['Oranges', 200, 140, 110],
             '113M': ['Mangoes', 100, 80, 50], '114O': ['Strawberries', 100, 90, 80],
             '115P': ['Blueberries', 95, 80, 75],
             '116Q': ['Green Teas', 250, 225, 200], '117R': ['Sparkling Water', 20, 14.50, 11],
             '118S': ['Dried Apricots',
                      270, 250, 230], '119T': ['Dried Figs', 100, 95, 90], '120U': ['Dried Prunes', 90, 85, 80],
             '121V': ['Almonds',
                      900, 870, 850], '122W': ['Cashews', 1000, 950, 910], '123X': ['Walnuts', 800, 770, 720],
             '124Y': ['Peanuts',
                      400, 380, 360], '125Z': ['Pecans', 350, 320, 300], '201A': ['Pistachios', 1200, 1180, 1160],
             '202B': ['Sunflower Seeds',
                      150, 112.50, 103.45], '203C': ['Sesame Seeds', 120.50, 110.25, 101.40],
             '204D': ['Whole Flaxseeds', 95.20, 90.45, 89.20]}

Registered_Cust = {'9500012345': ['Surian', 'AAA1001'], '9500023456': ['Nila', 'AAA1002'],
                   '9712300078': ['Arivazhagan', 'AAA1003'],
                   '9586233333': ['Nithin Kumar', 'AAA1004'], '6931245872': ['Aravind', 'AAA1005']}

ans = 'Y'
total_bill = 0
Cart = ''
Itemcodes = []
Items = []
Qualities = []
Quantities = []
Costings = []
while ans == 'Y':
    Item_Code = input('Enter Item code for item: ').upper()
    if Item_Code not in shop_bill:
        print('Product unavailable/ Incorrect code. Try again')
        continue
    Itemcodes.append(Item_Code)
    Items.append(shop_bill[Item_Code][0])
    Qual = int(input('Enter quality you want to buy (1/2/3): '))
    Qualities.append(Qual)
    Quant = float(input('Enter quantity you want to buy (in kgs): '))
    Quantities.append(Quant)
    Price = (shop_bill[Item_Code][Qual]) * Quant
    Costings.append(Price)
    total_bill += Price
    ans = input('Press Y to enter more items to cart: ').upper()
Phone = input('Enter mobile number: ')
if (len(Phone) != 10) or (Phone.isdecimal() == False):
    print('Incorrect mobile number- try again')
    Phone = input('Enter mobile number: ')
if Phone in Registered_Cust:
    print('The customer is registered\n')
    if total_bill > 5000:
        total_bill -= total_bill * 0.02188
        print('''Thankyou for being a loyal customer. You have earned cash rewards worth 1.2% of your total bill amount!
andd a discount of 1% \n''')
else:
    print('customer not registered\n')
if total_bill > 10000:
    total_bill -= total_bill * 0.01
    print(' You gained an additional 1% discount on your purchase!')
bill_data = {'Item Code': Itemcodes, 'Item Name': Items, 'Quality': Qualities, 'Amount': Quantities,
             'Item Cost': Costings}
bill_table = p.DataFrame(bill_data)
bill_table.style.set_table_styles([{'selector': '', 'props': [('border', '4px solid blue')]}])
print(tabulate(bill_table, headers='keys', tablefmt="pretty"))
html = bill_table.to_html()
txtfile = open('billing show.html', "w")
txtfile.write(html)
chillax=round(total_bill, 2)
chillbro=str(chillax)
txtfile.write("Your total bill is:"+chillbro)
txtfile.close()
print("Your total bill is:", round(total_bill, 2))
filename = 'file:///'+os.getcwd()+'/' + 'billing show.html'
webbrowser.open_new_tab(filename)

This is the html part这是 html 零件

    <h1 style="color:green;"
        ><em><b><marquee width="100%" direction="left" height="100px" scrollamount="25">ITEMS PRESENT IN VIT MART</marquee></b></em></h1>
    </head><link rel="stylesheet" type="text/css" href="style%20(3).css">
<body style="background-image:url('/static/img/shop_logo.jpeg'); background-repeat: no-repeat; background-position:300px; background-color:#ff6500;">
    <FORM  method="get|POST" action="C:\Users\Altair\source\repos\PythonApplication4\templates\final_layout_2.html">
<br>
    <br><b>Billing Counter:</b><br>
    <TABLE border="1" style="background-color:blanchedalmond">
  <TR>
      <TD><label>Enter item code</label></TD>
      <TD><input type="text" name="inputting_item_code" maxlength="7"></input></TD>
       </TR>
  <TR>
      <TD><label>Enter item quality</label></TD>
      <TD><input type="number" name="inputting_item_quality" =min="1" max="3"></input></TD>
       </TR>
    <TR>
        <TD><label>Enter quantity</label></TD>
      <TD><input name="inputting_item_quantity" type="number" ></input></TD>
       </TR><TR>
      <TR>
      <TD><label>Would you like to add more items to the cart?(Y/N)</label></TD>
      <TD><input type="text" name="inputting_more_item" maxlength="1" ></input></TD>
       </TR>
</TABLE>
           <p>Please enter your mobile number</p>
<P><INPUT TYPE="number" VALUE="mobile_number" NAME="customer_id_mobile" minlength="10" maxlength="10"></P>
<P><INPUT TYPE="SUBMIT" VALUE="Submit" NAME="B1"></P>
  
</FORM></body>
</html> ```

You need a web framework.您需要一个 web 框架。 See https://www.djangoproject.com/ which has all the features you requested:请参阅https://www.djangoproject.com/它具有您要求的所有功能:

  1. How to get input from HTML form and store it as a python variable ( https://docs.djangoproject.com/en/dev/topics/forms/#using-a-form-in-a-view )如何从 HTML 表单获取输入并将其存储为 python 变量( https://docs.djangoproject.com/en/dev/form-in-a-view/#using--a
  2. How to format the output HTML part ( https://docs.djangoproject.com/en/4.0/topics/templates/ )如何格式化 output HTML 部分( https://docs.djangoproject.com/en/4.0/topics/templates/
  3. How to ensure that if I wanted to add more items, the python for loop will also be applicable to both the python calculation part and HTML part ( https://docs.djangoproject.com/en/4.0/topics/templates/ ) How to ensure that if I wanted to add more items, the python for loop will also be applicable to both the python calculation part and HTML part ( https://docs.djangoproject.com/en/4.0/topics/templates/ )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM