get alco prediction and table working
	
		
			
	
		
	
	
		
	
		
			All checks were successful
		
		
	
	
		
			
				
	
				Build and Push Image / Build and push image (push) Has been skipped
				
			
		
		
	
	
				
					
				
			
		
			All checks were successful
		
		
	
	Build and Push Image / Build and push image (push) Has been skipped
				
			This commit is contained in:
		
							parent
							
								
									d4c1c76be4
								
							
						
					
					
						commit
						436836d4ac
					
				@ -8,3 +8,4 @@ bootstrap-flask
 | 
			
		||||
waitress
 | 
			
		||||
bokeh
 | 
			
		||||
pandas
 | 
			
		||||
duckdb
 | 
			
		||||
@ -1,6 +1,7 @@
 | 
			
		||||
from bokeh.core.enums import SpatialUnitsType
 | 
			
		||||
import mongo.build_db as beer_database
 | 
			
		||||
import mongo.query_db as beer_database_query
 | 
			
		||||
from table import table_builder
 | 
			
		||||
 | 
			
		||||
from flask import Flask, render_template, request, jsonify, redirect, session
 | 
			
		||||
from flask_wtf import FlaskForm, CSRFProtect
 | 
			
		||||
@ -14,6 +15,7 @@ from charts import BeerCharts
 | 
			
		||||
from bokeh.io import output_file, show
 | 
			
		||||
from bokeh.layouts import row
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
app = Flask(__name__)
 | 
			
		||||
app.secret_key = 'testsecret' #this value will change
 | 
			
		||||
 | 
			
		||||
@ -62,11 +64,16 @@ def index():
 | 
			
		||||
def updater():
 | 
			
		||||
    if 'logged_in' not in session:
 | 
			
		||||
        return redirect("/")
 | 
			
		||||
    #create_graphs()
 | 
			
		||||
    predicted_alc_table = table_builder.TableBuilder().table_build()
 | 
			
		||||
    tr_replace_string = '<tr align="center" style="border-bottom:1pt solid black;">'
 | 
			
		||||
    beer_html = predicted_alc_table.to_html(col_space='75px', index=False, 
 | 
			
		||||
                                            justify='center', border=3).replace('<tr>', tr_replace_string)
 | 
			
		||||
    query_db = beer_database_query.pool_query()
 | 
			
		||||
    query = query_db.get_top(10, "sg")
 | 
			
		||||
    form = dataForm()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    if form.validate_on_submit():
 | 
			
		||||
        database = beer_database.beer_data()
 | 
			
		||||
        new_record = {
 | 
			
		||||
@ -91,9 +98,11 @@ def updater():
 | 
			
		||||
            database.create_re_record(new_record["beer_run_id"], new_record["beer_type"], new_record["sg"],
 | 
			
		||||
                                       new_record["date"], final_run_value, new_record["comment"])
 | 
			
		||||
 | 
			
		||||
        return render_template("updater.html", list=query, form=form, success=True, updater_name = "Saucy Beer Maker")
 | 
			
		||||
        return render_template("updater.html", beer_data = beer_html 
 | 
			
		||||
                               , list=query, form=form, success=True, updater_name = "Saucy Beer Maker")
 | 
			
		||||
    else:
 | 
			
		||||
        return render_template("updater.html", list=query, form=form, sucess=False)
 | 
			
		||||
        return render_template("updater.html", beer_data = beer_html
 | 
			
		||||
                               , list=query, form=form, sucess=False)
 | 
			
		||||
 | 
			
		||||
# @app.route("/update_db", methods=["POST"])
 | 
			
		||||
# def beer_data_update():
 | 
			
		||||
 | 
			
		||||
@ -2,12 +2,13 @@ import sys
 | 
			
		||||
 | 
			
		||||
from mongo.get_conn import db_conn
 | 
			
		||||
import pandas as pd
 | 
			
		||||
import duckdb
 | 
			
		||||
 | 
			
		||||
class TableBuilder():
 | 
			
		||||
    def __init__(self) -> None:
 | 
			
		||||
        self.db = db_conn()
 | 
			
		||||
 | 
			
		||||
    def table_build(self, field, read_count=3, runs=5) -> pd.DataFrame:
 | 
			
		||||
    def table_build(self, limit=10) -> pd.DataFrame:
 | 
			
		||||
        data = self.db.beer_db
 | 
			
		||||
        data = data.find({}).sort("date", -1)
 | 
			
		||||
        df_dict = {}
 | 
			
		||||
@ -21,9 +22,28 @@ class TableBuilder():
 | 
			
		||||
            df_dict["sg"].append(record["sg"])
 | 
			
		||||
            df_dict["date"].append(record["date"])
 | 
			
		||||
            df_dict["final_reading"].append(record["final_reading"])
 | 
			
		||||
 | 
			
		||||
        df = pd.DataFrame(data=df_dict)
 | 
			
		||||
        df_sum = df.groupby('beer_run_id').agg({'sg': ['max', 'min']})
 | 
			
		||||
        return df_sum
 | 
			
		||||
        sql = f"""
 | 
			
		||||
            SELECT  x.beer_run_id as "Beer Run", 
 | 
			
		||||
                    max(sg)  as "Max",
 | 
			
		||||
                    min(sg)  as "Min"
 | 
			
		||||
            FROM df x
 | 
			
		||||
            JOIN
 | 
			
		||||
            (   SELECT DISTINCT beer_run_id 
 | 
			
		||||
                FROM df
 | 
			
		||||
                WHERE final_reading = 'True'
 | 
			
		||||
            ) y ON x.beer_run_id = y.beer_run_id
 | 
			
		||||
            GROUP BY x.beer_run_id
 | 
			
		||||
            ORDER BY x.beer_run_id desc
 | 
			
		||||
            LIMIT {limit}
 | 
			
		||||
        """
 | 
			
		||||
        df_sum = duckdb.sql(sql).df()
 | 
			
		||||
        sql = f"""
 | 
			
		||||
            SELECT  *, 
 | 
			
		||||
                    ROUND(((CAST (max AS INTEGER) - CAST(min AS INTEGER)) / 7.36) + 0.5, 2) AS "Alcohol Prediction"
 | 
			
		||||
            FROM df_sum
 | 
			
		||||
        """
 | 
			
		||||
        df_calc = duckdb.sql(sql).df()
 | 
			
		||||
        return df_calc
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -46,18 +46,15 @@
 | 
			
		||||
                                </div>
 | 
			
		||||
                            </div>
 | 
			
		||||
                        </td> -->
 | 
			
		||||
                        <td>
 | 
			
		||||
                            <div class="container">
 | 
			
		||||
                                <div class= "col-xs-12 col-sm-12 col-md-12">
 | 
			
		||||
                                    <!--<iframe style="width: 100vw; height: 40vh;" src="static/data_plot.html" frameborder="0" allowfullscreen>                                   
 | 
			
		||||
                                   <!--<iframe style="width: 100vw; height: 40vh;" src="static/data_plot.html" frameborder="0" allowfullscreen>                                   
 | 
			
		||||
                                    <!--<iframe style="width: 100vw;height: 80vh;position: relative;" src="static/data_plot.html" frameborder="0" allowfullscreen>-->
 | 
			
		||||
                                    </iframe>
 | 
			
		||||
                                </div>
 | 
			
		||||
                            </div>
 | 
			
		||||
                        </td>
 | 
			
		||||
                    </tr>
 | 
			
		||||
                </table>
 | 
			
		||||
            </div>
 | 
			
		||||
            <div class="col-md-6">
 | 
			
		||||
                {{ beer_data | safe }}
 | 
			
		||||
    
 | 
			
		||||
            </div>
 | 
			
		||||
        </div>
 | 
			
		||||
    </div>
 | 
			
		||||
  </div>
 | 
			
		||||
 | 
			
		||||
@ -2,4 +2,4 @@ import table.table_builder as table_builder
 | 
			
		||||
 | 
			
		||||
test = table_builder.TableBuilder()
 | 
			
		||||
 | 
			
		||||
print(test.table_build("sg"))
 | 
			
		||||
print(test.table_build())
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user