From 8e3e57e06e5df63acb02da2611f164b2102f5463 Mon Sep 17 00:00:00 2001
From: Russell Jarvis <colouredstatic@gmail.com>
Date: Mon, 2 Dec 2019 13:18:45 +1100
Subject: [PATCH] for plotly-dash forum ask for help

---
 OnlineApp/app.py   |  44 +++++++++++++-
 OnlineApp/dash7.py | 147 +++++++++++++++++----------------------------
 2 files changed, 97 insertions(+), 94 deletions(-)

diff --git a/OnlineApp/app.py b/OnlineApp/app.py
index 20e775a..53e3932 100644
--- a/OnlineApp/app.py
+++ b/OnlineApp/app.py
@@ -10,6 +10,41 @@ app = dash.Dash(__name__, server=server,external_stylesheets=external_stylesheet
 app.config.suppress_callback_exceptions = True
 
 
+image_filename = "figures/boxplot.png" # replace with your own image
+test_base64 = base64.b64encode(open(image_filename, 'rb').read())
+
+def fig_to_uri(in_fig, close_all=True, **save_args):
+    # type: (plt.Figure) -> str
+    """
+    Save a figure as a URI
+    :param in_fig:
+    :return:
+    """
+    out_img = BytesIO()
+    in_fig = open("figures/boxplot.png")
+    in_fig.savefig(out_img, format='png', **save_args)
+    if close_all:
+        in_fig.clf()
+        plt.close('all')
+    out_img.seek(0)  # rewind file
+    encoded = base64.b64encode(out_img.read()).decode("ascii").replace("\n", "")
+    return "data:image/png;base64,{}".format(encoded)
+
+def long_process(NAME):
+
+    #return
+    print('enters long process')
+    #NAME = "S S Phatak"
+    verbose = True
+
+    ar = online_app_backend.call_from_front_end(NAME,verbose=verbose)
+    print(ar)
+
+    axes,fig = bplot.plot_author(ar,NAME)
+    out_url = fig_to_uri(fig)
+    return out_url
+
+
 app.layout = html.Div(children=[
     html.H1(children='Hello Dash'),
 
@@ -21,14 +56,19 @@ app.layout = html.Div(children=[
         id='example-graph',
         figure={
             'data': [
-                {'x': [1, 2, 3], 'y': [4, 1, 2zz], 'type': 'bar', 'name': 'SF'},
+                {'x': [1, 2, 3], 'y': [4, 1, 2], 'type': 'bar', 'name': 'SF'},
                 {'x': [1, 2, 3], 'y': [2, 4, 5], 'type': 'bar', 'name': u'Montréal'},
             ],
             'layout': {
                 'title': 'Dash Data Visualization'
             }
         }
-    )
+    ),
+    html.Img(src=app.get_asset_url('figures/boxplot.png')),
+    #html.Div([html.Img(id = 'cur_plot', src = '')],
+    #         id='plot_div')
+    html.Img(src='data:image/png;base64,{}'.format(test_base64))
+
 ])
 
 if __name__ == '__main__':
diff --git a/OnlineApp/dash7.py b/OnlineApp/dash7.py
index f78a9d9..2e02dce 100644
--- a/OnlineApp/dash7.py
+++ b/OnlineApp/dash7.py
@@ -3,123 +3,86 @@ import dash
 import dash_core_components as dcc
 import dash_html_components as html
 from dash.dependencies import Input, Output, State
-
-
+import pickle
+import plotly.express as px
+import pandas as pd
+import numpy as np
 import datetime
 import time
-try:
-    import bplot
-    import online_app_backend
-except:
-    from SComplexity import online_app_backend
-    from SComplexity.OnlineApp import bplot
-
-import matplotlib.pyplot as plt
-import numpy as np
-from io import BytesIO
-import base64
-
-from io import BytesIO
-import matplotlib.pyplot as plt
-import base64
-import numpy as np
-import pickle
-
+# get wikipedia data
+with open('scraped_new.p','rb') as f:
+    texts = pickle.load(f)
+
+queries = set([t['query'] for t in texts ])
+temp = [t for t in texts if 'standard' in t.keys() and 'wikipedia' in t['link']]
+science = ['cancer','Vaccines','evolution','climate change','Transgenic','photosysnthesis','evolution','GMO']
+res = [t['standard'] for t in temp if t['query'] in science]
+mwp = np.mean(res)
+abstract_wiki = {'standard':mwp}
+
+def new_old(old,new):
+    old['Art_Corpus_Derived'] = True
+    new['Art_Corpus_Derived'] = False
+    newer = old.append(new)
+    return newer
 
 external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
 
-def fig_to_uri(in_fig, close_all=True, **save_args):
-    # type: (plt.Figure) -> str
-    """
-    Save a figure as a URI
-    :param in_fig:
-    :return:
-    """
-    out_img = BytesIO()
-    in_fig.savefig(out_img, format='png', **save_args)
-    if close_all:
-        in_fig.clf()
-        plt.close('all')
-    out_img.seek(0)  # rewind file
-    encoded = base64.b64encode(out_img.read()).decode("ascii").replace("\n", "")
-    return "data:image/png;base64,{}".format(encoded)
-
 def long_process(NAME):
+    with open('traingDats.p','rb') as f:
+        artcorpus = pickle.load(f)
+    with open('scraped_new.p','rb') as f:
+        new_author = pickle.load(f)
+
+    new_author = pd.DataFrame(new_author)
+    artcorpus = pd.DataFrame(artcorpus)
 
-    #return
     print('enters long process')
-    #NAME = "S S Phatak"
     verbose = True
-
-    ar = online_app_backend.call_from_front_end(NAME,verbose=verbose)
-    print(ar)
-
-    axes,fig = bplot.plot_author(ar,NAME)
-    out_url = fig_to_uri(fig)
-    return out_url
-
-#def update_graph(input_value, n_val):
-
-#    out_url = fig_to_uri(fig)
-#    return out_url
-
-
-# external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
+    # compute expensive code that I really want to use.
+    # import online_app_backend
+    # from SComplexity import online_app_backend
+    # ar = online_app_backend.call_from_front_end(NAME,verbose=verbose)
+    newer = new_old(artcorpus,ar)
+    plot = dcc.Graph(
+        id='example-graph',
+        figure = px.histogram(newer, x="standard", y="standard", color="Art_Corpus_Derived",
+                   marginal="box", # or violin, rug
+                   hover_data=newer.columns,width=900, height=900)
+    )
+    return plot
 
 app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
 
-
-
 app.layout = html.Div([
     html.Button(id='submit-button', n_clicks=0, children='Submit'),
-    dcc.Input(id='input-1-state', type='text', value='Montréal'),
-    html.Div(id='output-state'),
-    html.Div([html.Img(id = 'cur_plot', src = '')],
+    dcc.Input(id='input-1-state', type='text', value='enter author name'),
+    html.Div(id='newer'),
+    html.Div([html.Img(id = 'long_process', src = '')],
              id='plot_div'),
-    html.Div(children=[
-    html.H1(children='Hello Dash'),
+    html.H1(children='Author contest on science readability'),
     html.Div(children='''
-        Dash: A web application framework for Python.
-    '''),
-
-    dcc.Graph(
-        id='example-graph',
-        figure={
-            'data': [
-                {'x': [1, 2, 3], 'y': [4, 1, 2], 'type': 'bar', 'name': 'SF'},
-                {'x': [1, 2, 3], 'y': [2, 4, 5], 'type': 'bar', 'name': u'Montréal'},
-            ],
-            'layout': {
-                'title': 'Dash Data Visualization'
-            }
-        }
-    )
-    ])
+        Wikipedia on science, versus art corpus all biochem papers.
+    ''')
 ])
 @app.callback(
-    Output('cur_plot', 'children'),
+    Output('newer', 'children'),
     [Input('submit-button', 'n_clicks')],
     [State('input-1-state', 'value')]
 )
-def update_graph(n_clicks, input1):
+def update_graph(n_clicks, value):
     print(u'''
         The Button has been pressed {} times,
         Input 1 is "{}",
         and Input 2 is "{}"
-    '''.format(n_clicks, input1))
-    plot_div = long_process(input1)
-    return plot_div
-
-
-
-
-@app.callback(
-    Output("output-state", "children"),
-    [Input("input-1-state", "value")]
-)
-def update_output(input1):
-    return u'Input 1 is {0}'.format(input1)
-
+    '''.format(n_clicks))
+    print(u'''
+        The Button has been pressed {} times,
+        Input 1 is "{}",
+        and Input 2 is "{}"
+    '''.format(value))
+    newer = long_process(value)
+    return newer
 
 app.css.config.serve_locally = True
 app.scripts.config.serve_locally = True
-- 
GitLab