在django中重新加载模板变量而不刷新

xxhby3vn  于 2021-05-29  发布在  Spark
关注(0)|答案(0)|浏览(303)

当Spark流在view.py中完成时,我将自动更新模板变量而不刷新网页
视图.py

spark = SparkSession.builder.appName("Python Spark SQL basic example").config("spark.some.config.option", "some-value").getOrCreate()
schema = StructType().add("_c0", "integer").add("InvoiceNo", "string").add("Quantity","integer").add("InvoiceDate","date").add("UnitPrice","integer").add("CustomerID","double").add("TotalSales","integer")
INPUT_DIRECTORY = os.path.join(os.path.dirname(__file__), "../jsonFileInput")
OUTPUT_DIRECTORY = os.path.join(os.path.dirname(__file__), "../jsonFileOutput")
CHECKPOINT_DIRECTORY = os.path.join(os.path.dirname(__file__), "../writestream_checkpoints")
dataStream = spark.readStream.format("json").schema(schema).load(INPUT_DIRECTORY)

query = dataStream \
    .writeStream \
    .format("json") \
    .option("checkpointLocation",CHECKPOINT_DIRECTORY) \
    .option("path", OUTPUT_DIRECTORY) \
    .start()

def dashboardV2 (request):
    df = spark.read.format('json').schema(schema).option('header',True).load(OUTPUT_DIRECTORY)
    totalSales = codeData.totalSales(df)
    ncustomer = codeData.allCustomer(df)
    return render(request,'dashboardV2.html',{
        'totalSales': totalSales,
        'ncustomer': ncustomer
    })

我需要自动更新totalsales和ncustomer变量的模板时,流数据(df变量将读取触发流的json文件,并使用它在函数中计算totalsales和ncustomer变量)
模板.html

<!DOCTYPE html>
<html>
    <body>
        <label class="valueTotalSales2017">$ {{totalSales}} M</label>
        <label class="valueNumCus2017">{{ncustomer}}</label>
    </body>
</html>

代码数据.py

spark = SparkSession.builder.appName("Python Spark SQL basic example").config("spark.some.config.option", "some-value").getOrCreate()

def totalSales(df):
    total = df.select('TotalSales').groupby().sum().toPandas()
    total = total['sum(TotalSales)'][0]/1000000 # convert to millions
    total = "%.2f" % total   # trim 2 decimal
    return total

def allCustomer(df):
    ncus = df.select('CustomerID').distinct().count()
    return ncus

谢谢你的帮助。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题