Guest User

Untitled

a guest
Mar 26th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.47 KB | None | 0 0
  1. # Keeping this file as an example of Dash; can't really use Dash now since there are some missing features
  2. # for what we are trying to build and documentation is very poor at this point
  3.  
  4. # in order to run, install:
  5. # pip install dash==0.17.7
  6. # pip install dash-renderer==0.7.3
  7. # pip install dash-html-components==0.6.2
  8. # pip install dash-core-components==0.5.1
  9. # pip install plotly==2.0.11
  10.  
  11. import dash
  12. import dash_core_components as dcc
  13. import dash_html_components as html
  14. from dash.dependencies import Input, Output, State, Event
  15. # import plotly.graph_objs as go
  16. # import pandas as pd
  17. from metis.security import is_valid_user
  18. import requests
  19.  
  20. app = dash.Dash()
  21.  
  22. app.layout = html.Div(children=[
  23. # SCOPE header
  24. html.Div(children=[
  25. html.Img(src="logo.png", alt="Metis")
  26. ], style={'padding' : 7, 'text-align' : 'center'}),
  27. html.Hr(style={'width' : '30%'}),
  28. # content
  29. html.Div(children=[
  30. html.H3("Welcome to Metis", hidden=False, id="page_header"),
  31. html.H3("Rules", hidden=True, id='help_page'),
  32. # login form (can't seem to get Form to work properly so Div is used--button must be clicked with mouse)
  33. html.Div(children=[
  34. html.P(children=["Username: ", dcc.Input(type='text', id='username', placeholder='username')],
  35. id='user_field'),
  36. html.P(children=["Password: ", dcc.Input(type='password', id='password', placeholder='password')],
  37. id='password_field'),
  38. html.Button(children=['Login'], type='submit', id='login_button')
  39. ], style={'width' : '30%', 'margin' : '0 auto'}, id="login_form", hidden=False)
  40. ], style={'display' : 'block', 'text-align' : 'center', 'padding' : 2}),
  41. html.Br(),
  42. html.Hr(style={'width' : '30%'}),
  43. # footer
  44. html.Div(children=[
  45. html.Img(src="logo.png", alt="SCOPE Logo")
  46. ], style={'padding' : 7, 'text-align' : 'center'})
  47. ])
  48.  
  49. @app.callback(Output('page_header', 'children'),
  50. events=[Event('login_button', 'click')],
  51. state=[State('username', 'value'), State('password', 'value')])
  52. def login(username, password):
  53. if not username or not password:
  54. return html.Div("Please provide your username and password",
  55. style={'color' : 'red', 'text-align' : 'center'})
  56. elif is_valid_criteo_user(username, password):
  57. return "You have been logged in"
  58. else:
  59. return html.Div("Wrong username/password please try again.",
  60. style={'color' : 'red', 'text-align' : 'center'})
  61.  
  62. @app.callback(Output('user_field', 'hidden'), [Input('page_header', 'children')])
  63. def hide_username_box(page):
  64. if page == 'You have been logged in':
  65. return True
  66.  
  67. @app.callback(Output('password_field', 'hidden'), [Input('page_header', 'children')])
  68. def hide_password_box(page):
  69. if page == 'You have been logged in':
  70. return True
  71.  
  72. @app.callback(Output('login_button', 'hidden'), [Input('page_header', 'children')])
  73. def hide_login_button(page):
  74. if page == 'You have been logged in':
  75. return True
  76.  
  77. @app.callback(Output('page_header', 'hidden'), [Input('page_header', 'children')])
  78. def change_header(page):
  79. if page == 'You have been logged in':
  80. return True
  81.  
  82. @app.callback(Output('username', 'value'), [Input('page_header', 'children')])
  83. def clear_username_field():
  84. return ''
  85.  
  86. @app.callback(Output('password', 'value'), [Input('page_header', 'children')])
  87. def clear_password_field():
  88. return ''
  89.  
  90. @app.callback(Output('help_page', 'hidden'), [Input('page_header', 'hidden')])
  91. def show_help(logged_in):
  92. if logged_in:
  93. return False
  94. else:
  95. return True
  96.  
  97. if __name__ == "__main__":
  98. app.run_server(debug=True)
Add Comment
Please, Sign In to add comment