forked from null-jones/streamlit-plotly-events
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move directories around & prep for first release
- Loading branch information
null-jones
committed
Apr 20, 2021
1 parent
0dde436
commit d4c0fd1
Showing
15 changed files
with
224 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
recursive-include package/frontend/build * | ||
recursive-include src/streamlit_plotly_events/frontend/build * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,89 @@ | ||
# 🔥📊📣 Streamlit Plotly Events 📣📊🔥 | ||
Ever wanted to harness those awesome events from Plotly charts inside of Streamlit? So did I, so now you can! | ||
|
||
This is relatively trivial to accomplish, so I'd bet good money Streamlit is already working on this, but I can't wait :) | ||
![Example Image](example.gif) | ||
|
||
## Overview, TL;DR | ||
### Installation | ||
Install via Pip! | ||
|
||
```pip install streamlit-plotly-events``` | ||
|
||
### Usage | ||
Import the component, and use it like any other Streamlit custom component! | ||
```python | ||
import streamlit as st | ||
from streamlit_plotly_events import plotly_events | ||
|
||
# Writes a component similar to st.write() | ||
fig = px.line(x=[1], y=[1]) | ||
selected_points = plotly_events(fig) | ||
|
||
# Can write inside of things using with! | ||
with st.beta_expander('Plot'): | ||
fig = px.line(x=[1], y=[1]) | ||
selected_points = plotly_events(fig) | ||
|
||
# Select other Plotly events by specifying kwargs | ||
fig = px.line(x=[1], y=[1]) | ||
selected_points = plotly_events(fig, click_event=False, hover_event=True) | ||
``` | ||
|
||
What the component returns: | ||
``` | ||
Returns | ||
------- | ||
list of dict | ||
List of dictionaries containing point details (in case multiple overlapping points have been clicked). | ||
Details can be found here: | ||
https://plotly.com/javascript/plotlyjs-events/#event-data | ||
Format of dict: | ||
{ | ||
x: int (x value of point), | ||
y: int (y value of point), | ||
curveNumber: (index of curve), | ||
pointNumber: (index of selected point), | ||
pointIndex: (index of selected point) | ||
} | ||
``` | ||
|
||
## Events | ||
Currently, a number of plotly events can be enabled. They can be enabled/disabled using kwargs on the `plotly_event()` function. | ||
- **Click** `click_event` (defaults to `True`): Triggers event on mouse click of point | ||
- **Select** `select_event`: Triggers event when points have been lasso | ||
- **Hover** `hover_event`: Triggers event on mouse hover of point (**WARNING: VERY RESOURCE INTENSIVE**) | ||
|
||
# Contributing | ||
Please! I'm hardly a frontend developer! I think there's a bunch of amazing functionality we can add into streamlit/plotly!! | ||
|
||
This repo follows `black` formatting standards for the Python parts of the project. | ||
|
||
Follow the instructions on the `streamlit_components` [example repository](https://github.com/streamlit/component-template) to get up and running, or follow along below! | ||
|
||
### Quickstart | ||
|
||
* Ensure you have [Python 3.6+](https://www.python.org/downloads/), [Node.js](https://nodejs.org), and [npm](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm) installed. | ||
* Clone this repo. | ||
* Create a new Python virtual environment for the template: | ||
``` | ||
$ cd template | ||
$ python3 -m venv venv # create venv | ||
$ . venv/bin/activate # activate venv | ||
$ pip install streamlit # install streamlit | ||
$ pip install plotly # install plotly | ||
``` | ||
* Initialize and run the component template frontend: | ||
``` | ||
$ cd src/streamlit_plotly_events/frontend | ||
$ npm install # Install npm dependencies | ||
$ npm run start # Start the Webpack dev server | ||
``` | ||
* From a separate terminal, run the template's Streamlit app: | ||
``` | ||
$ cd src/streamlit_plotly_events | ||
$ . venv/bin/activate # activate the venv you created earlier | ||
$ streamlit run __init__.py # run the example server | ||
``` |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,14 +2,15 @@ | |
|
||
setuptools.setup( | ||
name="streamlit-plotly-events", | ||
version="0.0.1", | ||
version="0.0.5", | ||
author="Ellie Jones", | ||
author_email="[email protected]", | ||
description="Plotly chart component for Streamlit that also allows for events to bubble back up to Streamlit.", | ||
long_description="", | ||
long_description="Plotly chart component for Streamlit that also allows for events to bubble back up to Streamlit.", | ||
long_description_content_type="text/plain", | ||
url="", | ||
packages=setuptools.find_packages(), | ||
url="https://github.com/null-jones/streamlit-plotly-events", | ||
package_dir={"": "src"}, | ||
packages=setuptools.find_packages(where="src"), | ||
include_package_data=True, | ||
classifiers=[], | ||
python_requires=">=3.6", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
56 changes: 56 additions & 0 deletions
56
src/streamlit_plotly_events/frontend/src/StreamlitPlotlyEventsComponent.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { | ||
Streamlit, | ||
StreamlitComponentBase, | ||
withStreamlitConnection, | ||
} from "streamlit-component-lib" | ||
import React, { ReactNode } from "react" | ||
import Plot from 'react-plotly.js'; | ||
|
||
class StreamlitPlotlyEventsComponent extends StreamlitComponentBase { | ||
public render = (): ReactNode => { | ||
// Pull Plotly object from args and parse | ||
const plot_obj = JSON.parse(this.props.args["plot_obj"]); | ||
const override_height = this.props.args["override_height"]; | ||
|
||
// Event booleans | ||
const click_event = this.props.args["click_event"]; | ||
const select_event = this.props.args["select_event"]; | ||
const hover_event = this.props.args["hover_event"]; | ||
|
||
Streamlit.setFrameHeight(override_height); | ||
return ( | ||
<div | ||
style={{"height": override_height}} | ||
className="stPlotlyChart" | ||
> | ||
<Plot | ||
data={plot_obj.data} | ||
layout={plot_obj.layout} | ||
onClick={click_event ? this.plotlyEventHandler : function(){}} | ||
onSelected={select_event ? this.plotlyEventHandler : function(){}} | ||
onHover={hover_event ? this.plotlyEventHandler : function(){}} | ||
/> | ||
</div> | ||
) | ||
} | ||
|
||
/** Click handler for plot. */ | ||
private plotlyEventHandler = (data: any) => { | ||
// Build array of points to return | ||
var clickedPoints: Array<any> = []; | ||
data.points.forEach(function (arrayItem: any) { | ||
clickedPoints.push({ | ||
x: arrayItem.x, | ||
y: arrayItem.y, | ||
curveNumber: arrayItem.curveNumber, | ||
pointNumber: arrayItem.pointNumber, | ||
pointIndex: arrayItem.pointIndex | ||
}) | ||
}); | ||
|
||
// Return array as JSON to Streamlit | ||
Streamlit.setComponentValue(JSON.stringify(clickedPoints)) | ||
} | ||
} | ||
|
||
export default withStreamlitConnection(StreamlitPlotlyEventsComponent) |
File renamed without changes.
File renamed without changes.
File renamed without changes.
50 changes: 0 additions & 50 deletions
50
streamlit-plotly-events/frontend/src/StreamlitPlotlyEventsComponent.tsx
This file was deleted.
Oops, something went wrong.