# uncomment the following line to install/upgrade the PixieDust library
# ! pip install pixiedust --user --upgrade
import pixiedust

"""
Explanation: What are PixieApps?
PixieApps are Python classes used to write UI elements for your analytics, and they run directly in a Jupyter notebook. Learn more
End of explanation
"""


from pixiedust.display.app import *
@PixieApp
class HelloWorldPixieApp:
    @route()
    def main(self):
        return"""
            <input pd_options="clicked=true" type="button" value="Click Me">
        """
    @route(clicked="true")
    def _clicked(self):
        return """
            <input pd_options="clicked=false" type="button" value="You Clicked, Now Go back">
        """
#run the app
HelloWorldPixieApp().run(runInDialog='false')

"""
Explanation: Hello World
The following sample PixieApp provides two simple views and their associated routes. The first view (default view) creates a button that, when clicked, will invoke the second view. The second view creates another button that redirects to the first view, and so on and so forth.
End of explanation
"""


from pixiedust.display.app import *
@PixieApp
class HelloWorldPixieAppWithData:
    @route()
    def main(self):
        return"""
        <div class="row">
           <div class="col-sm-2">
              <input pd_options="handlerId=dataframe"
                     pd_entity
                     pd_target="target{{prefix}}"
                     type="button" value="Preview Data">
           </div>
           <div class="col-sm-10" id="target{{prefix}}"/>
        </div>
        """
#Create dataframe
df = SQLContext(sc).createDataFrame(
[(2010, 'Camping Equipment', 3, 200),(2010, 'Camping Equipment', 10, 200),(2010, 'Golf Equipment', 1, 240),
 (2010, 'Mountaineering Equipment', 1, 348),(2010, 'Outdoor Protection',2,200),(2010, 'Personal Accessories', 2, 200),
 (2011, 'Camping Equipment', 4, 489),(2011, 'Golf Equipment', 5, 234),(2011, 'Mountaineering Equipment',2, 123),
 (2011, 'Outdoor Protection', 4, 654),(2011, 'Personal Accessories', 2, 234),(2012, 'Camping Equipment', 5, 876),
 (2012, 'Golf Equipment', 5, 200),(2012, 'Mountaineering Equipment', 3, 156),(2012, 'Outdoor Protection', 5, 200),
 (2012, 'Personal Accessories', 3, 345),(2013, 'Camping Equipment', 8, 987),(2013, 'Golf Equipment', 5, 434),
 (2013, 'Mountaineering Equipment', 3, 278),(2013, 'Outdoor Protection', 8, 134),(2013,'Personal Accessories',4, 200)],
 ["year","zone","unique_customers", "revenue"])

#run the app
HelloWorldPixieAppWithData().run(df, runInDialog='false')

"""
Explanation: <hr>
Hello World with Data
The following sample PixieApp simplifies the mechanics of loading and visualizing data. When run successfully, you’ll see a preview button appear in the same area as your Jupyter input cell. Clicking the button will display the data as a table.
End of explanation
"""


