示例#1
0
    def evaluationDisplay():
        evaluation = EvaluationSchema.EvaluationContext.lookupOrCreate()

        # force us to redraw if anything changes in terms of the horizontal width
        # because right now the plotly charts aren't smart enough to do this.
        cells.sessionState().showNavTree
        cells.sessionState().showEditor
        cells.sessionState().showEvaluation

        if not evaluation:
            return None

        return cells.Subscribed(
                lambda:
                    cells.Card("Waiting for backend...") if evaluation.state == "Dirty" else
                    cells.Card("Backend computing...") if evaluation.state == "Calculating" else
                    cells.Traceback(evaluation.error) if evaluation.error is not None else
                    cells.Tabs(
                        Displays=cells.Card(
                            cells.Subscribed(
                                lambda: ResearchFrontend.displaysDisplay(evaluation).tagged("RFE_Displays")
                                )
                            ),
                        Variables=cells.Card(
                            cells.Subscribed(
                                lambda: DisplayForVariables.variablesDisplay(evaluation).tagged("RFE_Variables")
                                )
                            )
                        ).tagged("DisplayTabCell")
                ).overflow("auto").width("100%")
def displayForObject(display):
    obj = display.object

    if isinstance(obj, types.ModuleType):
        return cells.Card(f"Module {obj.__name__}",
                          header=display.title or None)
    if isinstance(obj, types.FunctionType):
        return cells.Card(f"Function {obj.__qualname__}",
                          header=display.title or None)
    if isinstance(obj, type):
        return cells.Card(f"Type {obj.__qualname__}",
                          header=display.title or None)

    return cells.Card(f"Something else: {obj}", header=display.title or None)
示例#3
0
    def navDisplay():
        def projectView(project):
            expander = cells.Expands(
                closed=ResearchFrontend.projectLine(project),
                open=ResearchFrontend.projectLine(project) +
                    cells.SubscribedSequence(
                        lambda: sorted(Module.lookupAll(project=project), key=lambda m: m.name),
                        lambda m: ResearchFrontend.moduleLine(m)
                        )
                ).tagged(f"RFE_ProjectExpander_{project._identity}")

            def onProjectSelectChanged():
                if (cells.sessionState().selected_module and cells.sessionState().selected_module.exists()
                        and cells.sessionState().selected_module.project == project and not expander.isExpanded):
                    expander.isExpanded = True
                    expander.markDirty()

            return expander + cells.Subscribed(onProjectSelectChanged)

        return cells.Card(
            cells.SubscribedSequence(
                lambda: sorted(Project.lookupAll(), key=lambda p: p.name),
                projectView
                ) +
                cells.Code("\n\n\n\n") +
                cells.Button("New Project", ResearchFrontend.createNewProject).tagged("RFE_NewProjectButton")
            ).width(400)
def displayForDisplay(display):
    allDisplays = cells.Sequence(list(display.displays))

    result = cells.Card(allDisplays,
                        header=display.title if display.title else None)

    return result
示例#5
0
    def editorDisplay():
        module = cells.sessionState().selected_module

        if module is None or not module.exists():
            return cells.Card("Please select a module")

        def onEnter(buffer, selection):
            module.update(buffer)
            module.mark()

            evaluation = EvaluationSchema.EvaluationContext.lookupOrCreate()

            evaluation.request(module, None)

        def onExecuteSelected(buffer, selection):
            module.update(buffer)

            evaluation = EvaluationSchema.EvaluationContext.lookupOrCreate()

            if isinstance(selection,dict):
                selection = CodeSelection.fromAceEditorJson(selection)
                selectedText = selection.slice(buffer)
            else:
                selectedText = None

            evaluation.request(module, selectedText)

        def onTextChange(buffer, selection):
            module.update(buffer)

        ed = cells.CodeEditor(
            keybindings={'Enter': onEnter, 'Space': onExecuteSelected},
            fontSize=14,
            minLines=50,
            onTextChange=onTextChange
            ).height('calc(100vh - 110px)')

        def onCodeChange():
            """Executing this code in a 'subscribed' forces us to check whether the editor
            is out of sync with the current buffer. If so, we set the buffer.

            Any time the buffer changes because a user changes it, we should be forcing
            'current_buffer' to reflect that change immediately. Otherwise this code
            will execute and force it to be the same again.
            """
            if ed.getContents() != module.current_buffer:
                ed.setContents(module.current_buffer)

        return ed.width(1200) + cells.Subscribed(onCodeChange)
示例#6
0
    def serviceDisplay(serviceObject,
                       instance=None,
                       objType=None,
                       queryArgs=None):
        cells.ensureSubscribedType(TaskStatus, lazy=True)

        return cells.Card(
            cells.Subscribed(lambda: cells.Text("Total Tasks: %s" % len(
                TaskStatus.lookupAll()))) +
            cells.Subscribed(lambda: cells.Text("Working Tasks: %s" % len(
                TaskStatus.lookupAll(state='Working')))) + cells.Subscribed(
                    lambda: cells.Text("WaitingForSubtasks Tasks: %s" % len(
                        TaskStatus.lookupAll(state='WaitForSubtasks')))) +
            cells.Subscribed(lambda: cells.Text("Unassigned Tasks: %s" % len(
                TaskStatus.lookupAll(state='Unassigned')))))
def displayForPrint(display):
    return cells.Card(cells.Subscribed(
        lambda: cells.Code(display.str).tagged("DatasetDisplay")),
                      header=display.title or None)
def displayForPlot(display):
    # grab a context object, which tells us how we should filter our cube data for display purposes.
    # this must be pushed on the stack above us for us to display properly.
    datasetStatesUnnamed = display.args
    datasetStatesNamed = display.kwargs

    def downsamplePlotData(linePlot):
        data = {}

        if len(datasetStatesUnnamed) <= 1:
            for ds in datasetStatesUnnamed:
                data.update(makePlotData(ds, "", "series"))
        else:
            for i, ds in enumerate(datasetStatesUnnamed):
                data.update(makePlotData(ds, "", "series_" + str(i + 1)))

        for name, ds in datasetStatesNamed.items():
            data.update(makePlotData(ds, name + ":", name))

        with Timer("Downsampling plot data"):
            #because of slow connection speeds, lets not send more than MAX_POINTS_PER_CHART points total
            seriesCount = len(data)
            if seriesCount == 0:
                return data

            #first, restrict the dataset to what the xy can hold
            if linePlot.curXYRanges.get() is not None:
                minX, maxX = linePlot.curXYRanges.get()[0]

                for series in data:
                    dim = 'timestamp' if 'timestamp' in data[series] else 'x'
                    indexLeft = (max(
                        0, data[series][dim].searchsorted(minX -
                                                          (maxX - minX) / 2))
                                 if minX is not None else 0)
                    indexRight = (min(
                        data[series][dim].searchsorted(
                            maxX + (maxX - minX) / 2, 'right'),
                        len(data[series][dim])) if maxX is not None else 0)

                    data[series][dim] = data[series][dim][indexLeft:indexRight]
                    data[series]['y'] = data[series]['y'][indexLeft:indexRight]

            #now check our output point count
            totalPoints = sum(len(data[series]['y']) for series in data)

            colorIx = 0

            if totalPoints > 10000 or candlestick.get():
                with Timer("Downsampling %s total points", totalPoints):
                    if candlestick.get():
                        downsampleRatio = int(
                            numpy.ceil(totalPoints / (500 * len(data))))
                    else:
                        downsampleRatio = int(
                            numpy.ceil(totalPoints / (2000 * len(data))))

                    for series in data:
                        dim = 'timestamp' if 'timestamp' in data[
                            series] else 'x'

                        samplePoints = numpy.arange(
                            len(data[series][dim]) //
                            downsampleRatio) * downsampleRatio

                        if candlestick.get():
                            # take the average of the points in the middle
                            data[series][dim] = (
                                numpy.add.reduceat(data[series][dim],
                                                   samplePoints) /
                                numpy.add.reduceat(data[series][dim] * 0 + 1,
                                                   samplePoints))
                            data[series]['open'] = data[series]['y'][
                                samplePoints[:-1]]
                            data[series]['close'] = data[series]['y'][
                                samplePoints[1:] - 1]
                            data[series]['high'] = numpy.maximum.reduceat(
                                data[series]['y'], samplePoints)
                            data[series]['low'] = numpy.minimum.reduceat(
                                data[series]['y'], samplePoints)
                            data[series]['decreasing'] = data[series][
                                'increasing'] = {
                                    'line': {
                                        'color': nthColor(colorIx)
                                    }
                                }
                            colorIx += 1
                            del data[series]['y']
                            data[series]['type'] = 'candlestick'
                        else:
                            data[series]['line'] = {'color': nthColor(colorIx)}
                            data[series][dim] = data[series][dim][
                                samplePoints[1:] - 1]
                            data[series]['y'] = data[series]['y'][
                                samplePoints[1:] - 1]
                            colorIx += 1

            else:
                for series in data:
                    data[series]['line'] = {'color': nthColor(colorIx)}
                    colorIx += 1

        return data

    def makePlotData(toShow, prefix, emptySeriesName):
        return {emptySeriesName: {'x': numpy.arange(len(toShow)), 'y': toShow}}

    showChart = cells.Slot(True)
    candlestick = cells.Slot(False)

    def cardContents():
        if showChart.get():
            xySlot = cells.Slot()

            return cells.Plot(downsamplePlotData, xySlot=xySlot).width("100%")
        else:
            seq = []
            for plottedSet in list(display.unnamed) + list(
                    display.named.values()):
                seq.append(
                    research_app.DisplayForDataset.displayForDataset(
                        DatasetDisplay.Dataset(dataset=plottedSet)))
            return cells.Sequence(seq)

    res = cells.Card(
        cells.Subscribed(cardContents),
        header=cells.HeaderBar(
            [cells.Text(display.title)] if display.title else [], [], [
                cells.Subscribed(lambda: cells.Button(
                    "Show Candlestick",
                    lambda: candlestick.set(not candlestick.get()),
                    active=candlestick.get()) if showChart.get() else None),
                cells.Subscribed(lambda: cells.ButtonGroup([
                    cells.Button(cells.Octicon("graph"),
                                 lambda: showChart.set(True),
                                 active=showChart.get()),
                    cells.Button(cells.Octicon("three-bars"),
                                 lambda: showChart.set(False),
                                 active=not showChart.get())
                ]))
            ]))

    return res
示例#9
0
    def serviceDisplay(serviceObject,
                       instance=None,
                       objType=None,
                       queryArgs=None):
        cells.ensureSubscribedType(Configuration)
        cells.ensureSubscribedType(State)

        c = Configuration.lookupAny()

        if not c:
            return cells.Card("No configuration defined for  AWS")

        def bootCountSetter(state, ct):
            def f():
                state.desired = ct

            return f

        def bootCountSetterSpot(state, ct):
            def f():
                state.spot_desired = ct

            return f

        return cells.Grid(
            colFun=lambda: [
                'Instance Type', 'COST', 'RAM', 'CPU', 'Booted', 'Desired',
                'SpotBooted', 'SpotDesired', 'ObservedLimit',
                'CapacityConstrained', 'Spot-us-east-1', 'a', 'b', 'c', 'd',
                'e', 'f'
            ],
            rowFun=lambda: sorted([
                x for x in State.lookupAll()
                if x.instance_type in instance_types_to_show
            ],
                                  key=lambda s: s.instance_type),
            headerFun=lambda x: x,
            rowLabelFun=None,
            rendererFun=lambda s, field: cells.Subscribed(
                lambda: s.instance_type
                if field == 'Instance Type' else s.booted if field == 'Booted'
                else cells.Dropdown(s.desired, [
                    (str(ct), bootCountSetter(s, ct))
                    for ct in list(range(10)) + list(range(10, 101, 10))
                ]) if field == 'Desired' else s.spot_booted if field ==
                'SpotBooted' else cells.Dropdown(s.spot_desired, [
                    (str(ct), bootCountSetterSpot(s, ct))
                    for ct in list(range(10)) + list(range(10, 101, 10))
                ]) if field == 'SpotDesired' else
                ("" if s.observedLimit is None else s.observedLimit)
                if field == 'ObservedLimit' else
                ("Yes" if s.capacityConstrained else "")
                if field == 'CapacityConstrained' else valid_instance_types[
                    s.instance_type]['COST'] if field == 'COST' else
                valid_instance_types[s.instance_type]['RAM'] if field == 'RAM'
                else valid_instance_types[s.instance_type]['CPU'] if field ==
                'CPU' else s.spotPrices.get('us-east-1' + field, "")
                if field in 'abcdef' else "")) + cells.Card(
                    cells.Text("db_hostname = " + str(c.db_hostname)) +
                    cells.Text("db_port = " + str(c.db_port)) +
                    cells.Text("region = " + str(c.region)) +
                    cells.Text("vpc_id = " + str(c.vpc_id)) +
                    cells.Text("subnet = " + str(c.subnet)) +
                    cells.Text("security_group = " + str(c.security_group)) +
                    cells.Text("keypair = " + str(c.keypair)) +
                    cells.Text("worker_name = " + str(c.worker_name)) +
                    cells.Text("worker_iam_role_name = " +
                               str(c.worker_iam_role_name)) +
                    cells.Text("docker_image = " + str(c.docker_image)) +
                    cells.Text("defaultStorageSize = " +
                               str(c.defaultStorageSize)) +
                    cells.Text("max_to_boot = " + str(c.max_to_boot)))