├── .gitignore ├── .pre-commit-config.yaml ├── CONTRIBUTING.md ├── LICENSE ├── MANIFEST.in ├── README.md ├── assets ├── faces.png ├── facesandtext.png ├── thumbs.png ├── thumbsandtext.png └── thumbsstart.png ├── pyproject.toml ├── requirements.txt ├── setup.py └── streamlit_feedback ├── __init__.py ├── examples.py └── frontend ├── .env ├── .prettierrc ├── package-lock.json ├── package.json ├── public ├── bootstrap.min.css └── index.html ├── src ├── FacesFeedback.js ├── FacesWithQualiFeedback.js ├── MyComponent.js ├── ThumbsFeedback.js ├── ThumbsWithQualiFeedback.js ├── feedback.js ├── index.js └── react-app-env.d.ts └── tsconfig.json /.gitignore: -------------------------------------------------------------------------------- 1 | venv/ 2 | streamlit_feedback/frontend/node_modules 3 | streamlit_feedback/frontend/build 4 | *.egg-info/ 5 | build/ 6 | dist/ 7 | .streamlit/secrets.toml 8 | __pycache__/ 9 | -------------------------------------------------------------------------------- /.pre-commit-config.yaml: -------------------------------------------------------------------------------- 1 | repos: 2 | - repo: https://github.com/psf/black 3 | rev: 22.3.0 4 | hooks: 5 | - id: black 6 | args: [--preview] # forces splitting of long strings to respect line length 7 | - repo: https://github.com/pycqa/isort 8 | rev: 5.11.5 9 | hooks: 10 | - id: isort 11 | - repo: https://github.com/pre-commit/mirrors-mypy 12 | rev: "v0.950" 13 | hooks: 14 | - id: mypy 15 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | 2 | # Contributing to Streamlit-feedback 3 | 4 | First off, thanks for taking the time to contribute! ❤️ 5 | 6 | All types of contributions are encouraged and valued. See the [Table of Contents](#table-of-contents) for different ways to help and details about how this project handles them. Please make sure to read the relevant section before making your contribution. It will make it a lot easier for us maintainers and smooth out the experience for all involved. The community looks forward to your contributions. 🎉 7 | 8 | > And if you like the project, but just don't have time to contribute, that's fine. There are other easy ways to support the project and show your appreciation, which we would also be very happy about: 9 | > - Star the project ⭐ 10 | > - Tweet / LinkedIn post about it 11 | > - Get in touch with us on our [website](https://trubrics.com/) 12 | > - Refer this project in your project's readme 13 | > - Mention the project at local meetups and tell your friends/colleagues 14 | 15 | 16 | ## Table of Contents 17 | 18 | - [I Have a Question](#i-have-a-question) 19 | - [I Want To Contribute](#i-want-to-contribute) 20 | - [Reporting Bugs](#reporting-bugs) 21 | - [Suggesting Enhancements](#suggesting-enhancements) 22 | - [Your First Contribution](#your-first-contribution) 23 | 24 | 25 | 26 | ## I Have a Question 27 | 28 | > If you want to ask a question, we assume that you have read the available [README.md](./README.md). 29 | 30 | Before you ask a question, it is best to search for existing [Issues](https://github.com/trubrics/streamlit-feedback/issues) that might help you. In case you have found a suitable issue and still need clarification, you can write your question in this issue. It is also advisable to search the internet for answers first. 31 | 32 | If you then still feel the need to ask a question and need clarification, we recommend the following: 33 | 34 | - Open an [Issue](https://github.com/trubrics/streamlit-feedback/issues/new). 35 | - Provide as much context as you can about what you're running into. 36 | - Provide project and platform versions (python version, streamlit-feedback package version, etc), depending on what seems relevant. 37 | 38 | We will then take care of the issue as soon as possible. 39 | 40 | 54 | 55 | ## I Want To Contribute 56 | 57 | > ### Legal Notice 58 | > When contributing to this project, you must agree that you have authored 100% of the content, that you have the necessary rights to the content and that the content you contribute may be provided under the project license. 59 | 60 | ### Reporting Bugs 61 | 62 | 63 | #### Before Submitting a Bug Report 64 | 65 | A good bug report shouldn't leave others needing to chase you up for more information. Therefore, we ask you to investigate carefully, collect information and describe the issue in detail in your report. Please complete the following steps in advance to help us fix any potential bug as fast as possible. 66 | 67 | - Make sure that you are using the latest version. 68 | - Determine if your bug is really a bug and not an error on your side e.g. using incompatible environment components/versions. If you are looking for support, you might want to check [this section](#i-have-a-question). 69 | - To see if other users have experienced (and potentially already solved) the same issue you are having, check if there is not already a bug report existing for your bug or error in the [bug tracker](https://github.com/trubrics/streamlit-feedback/issues). 70 | - Also make sure to search the internet (including Stack Overflow) to see if users outside of the GitHub community have discussed the issue. 71 | - Collect information about the bug: 72 | - Stack trace (Traceback) 73 | - OS, Platform and Version (Windows, Linux, macOS, x86, ARM) 74 | - Version of the interpreter, compiler, SDK, runtime environment, package manager, depending on what seems relevant. 75 | - Possibly your input and the output 76 | - Can you reliably reproduce the issue? And can you also reproduce it with older versions? 77 | 78 | 79 | #### How Do I Submit a Good Bug Report? 80 | 81 | > You must never report security related issues, vulnerabilities or bugs including sensitive information to the issue tracker, or elsewhere in public. Instead sensitive bugs must be sent by email to . 82 | 83 | 84 | We use GitHub issues to track bugs and errors. If you run into an issue with the project: 85 | 86 | - Open an [Issue](https://github.com/trubrics/streamlit-feedback/issues/new). (Since we can't be sure at this point whether it is a bug or not, we ask you not to talk about a bug yet and not to label the issue.) 87 | - Explain the behavior you would expect and the actual behavior. 88 | - Please provide as much context as possible and describe the *reproduction steps* that someone else can follow to recreate the issue on their own. This usually includes your code. For good bug reports you should isolate the problem and create a reduced test case. 89 | - Provide the information you collected in the previous section. 90 | 91 | Once it's filed: 92 | 93 | - The project team will label the issue accordingly. 94 | - A team member will try to reproduce the issue with your provided steps. If there are no reproduction steps or no obvious way to reproduce the issue, the team will ask you for those steps and mark the issue as `needs-repro`. Bugs with the `needs-repro` tag will not be addressed until they are reproduced. 95 | - If the team is able to reproduce the issue, it will be marked `needs-fix`, as well as possibly other tags (such as `critical`), and the issue will be left to be [implemented by someone](#your-first-code-contribution). 96 | 97 | 98 | 99 | 100 | ### Suggesting Enhancements 101 | 102 | This section guides you through submitting an enhancement suggestion, **including completely new features and minor improvements to existing functionality**. Following these guidelines will help maintainers and the community to understand your suggestion and find related suggestions. 103 | 104 | 105 | #### Before Submitting an Enhancement 106 | 107 | - Make sure that you are using the latest version. 108 | - Read the [README.md](./README.md) carefully and find out if the functionality is already covered, maybe by an individual configuration. 109 | - Perform a [search](https://github.com/trubrics/streamlit-feedback/issues) to see if the enhancement has already been suggested. If it has, add a comment to the existing issue instead of opening a new one. 110 | - Find out whether your idea fits with the scope and aims of the project. It's up to you to make a strong case to convince the project's developers of the merits of this feature. Keep in mind that we want features that will be useful to the majority of our users and not just a small subset. If you're just targeting a minority of users, consider writing an add-on/plugin library. 111 | 112 | 113 | #### How Do I Submit a Good Enhancement Suggestion? 114 | 115 | Enhancement suggestions are tracked as [GitHub issues](https://github.com/trubrics/streamlit-feedback/issues). 116 | 117 | - Use a **clear and descriptive title** for the issue to identify the suggestion. 118 | - Provide a **step-by-step description of the suggested enhancement** in as many details as possible. 119 | - **Describe the current behavior** and **explain which behavior you expected to see instead** and why. At this point you can also tell which alternatives do not work for you. 120 | - You may want to **include screenshots and animated GIFs** which help you demonstrate the steps or point out the part which the suggestion is related to. You can use [this tool](https://www.cockos.com/licecap/) to record GIFs on macOS and Windows, and [this tool](https://github.com/colinkeenan/silentcast) or [this tool](https://github.com/GNOME/byzanz) on Linux. 121 | - **Explain why this enhancement would be useful** to most Trubrics users. You may also want to point out the other projects that solved it better and which could serve as inspiration. 122 | 123 | 124 | 125 | ### Your First Contribution 126 | - Fork this repository 127 | - After cloning your fork, create a feature branch from the main branch with `git checkout -b my_feature_name` 128 | - Create a virtual environment and run `pip install -r requirements.txt` to install all relevant packages 129 | - Go ahead and add your changes, making sure to include 130 | - type hints 131 | - unit tests 132 | - docstrings 133 | - Run your linter with ` pre-commit run --all-files` 134 | - Push your code & documentation and submit a PR to the `main` branch 135 | - Once you have reviewed your PR and merged in to your `main` branch, make a PR into the base repository 136 | 137 | 138 | ## Attribution 139 | This guide is based on the **contributing-gen**. [Make your own](https://github.com/bttger/contributing-gen)! 140 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2023 Trubrics Ltd 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 8 | -------------------------------------------------------------------------------- /MANIFEST.in: -------------------------------------------------------------------------------- 1 | recursive-include streamlit_feedback/frontend/build * 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # streamlit-feedback 2 | 3 | A Streamlit component to add **user feedback** to your apps. 4 | 5 | 6 | 7 | ```python 8 | from streamlit_feedback import streamlit_feedback 9 | feedback = streamlit_feedback(feedback_type="thumbs") 10 | feedback 11 | ``` 12 | 13 | ## Examples 14 | 15 | [Here](streamlit_feedback/examples.py) are many examples of how the feedback component can be added to your app. Each function represents a different app. 16 | 17 | > [!IMPORTANT] 18 | > The `streamlit_feedback` component triggers a page reload when submitted, this is how streamlit components work. The `on_submit` function is only then run when your app reaches the `streamlit-feedback()` call on the rerun. 19 | 20 | 21 | ## Install 22 | 23 | ```sh 24 | pip install streamlit-feedback 25 | ``` 26 | 27 | ## Usage 28 | It can be used with these parameters: 29 | 30 | ```python 31 | def streamlit_feedback( 32 | feedback_type, 33 | optional_text_label=None, 34 | max_text_length=None, 35 | review_on_positive=True, 36 | disable_with_score=None, 37 | on_submit=None, 38 | args=(), 39 | kwargs={}, 40 | align="flex-end", 41 | key=None, 42 | ): 43 | """Create a new instance of "streamlit_feedback". 44 | 45 | Parameters 46 | ---------- 47 | feedback_type: str 48 | The type of feedback; "thumbs" or "faces". 49 | optional_text_label: str or None 50 | An optional label to add as a placeholder to the textbox. 51 | If None, the "thumbs" or "faces" will not be accompanied by textual feedback. 52 | max_text_length: int or None 53 | Defaults to None. If set, enables the multi-line functionality and determines the maximum characters the textbox allows. Else, displays the default one-line textbox. 54 | disable_with_score: str 55 | An optional score to disable the component. Must be a "thumbs" emoji or a "faces" emoji. Can be used to pass state from one component to another. 56 | review_on_positive: bool 57 | Defaults to True. When set to False, it only asks for textual feedback in case of a negative review, i.e., "thumbs down" or less than the happiest "faces" emoji. 58 | on_submit: callable 59 | An optional callback invoked when feedback is submitted. This function must accept at least one argument, the feedback response dict, 60 | allowing you to save the feedback to a database for example. Additional arguments can be specified using `args` and `kwargs`. 61 | args: tuple 62 | Additional positional arguments to pass to `on_submit`. 63 | kwargs: dict 64 | Additional keyword arguments to pass to `on_submit`. 65 | align: str 66 | Where to align the feedback component; "flex-end", "center" or "flex-start". 67 | key: str or None 68 | An optional key that uniquely identifies this component. If this is 69 | None, and the component's arguments are changed, the component will 70 | be re-mounted in the Streamlit frontend and lose its current state. 71 | 72 | Returns 73 | ------- 74 | dict 75 | The user response, with the feedback_type, score and text fields. If on_submit returns a value, this value will be returned by the component. 76 | 77 | """ 78 | ``` 79 | 80 | Here are some more examples: 81 | 82 | ```python 83 | from streamlit_feedback import streamlit_feedback 84 | feedback = streamlit_feedback( 85 | feedback_type="thumbs", 86 | optional_text_label="[Optional] Please provide an explanation", 87 | ) 88 | feedback 89 | ``` 90 | 91 | 92 | 93 | --- 94 | 95 | ```python 96 | from streamlit_feedback import streamlit_feedback 97 | feedback = streamlit_feedback(feedback_type="faces") 98 | feedback 99 | ``` 100 | 101 | 102 | 103 | --- 104 | 105 | ```python 106 | from streamlit_feedback import streamlit_feedback 107 | feedback = streamlit_feedback( 108 | feedback_type="faces", 109 | optional_text_label="[Optional] Please provide an explanation", 110 | ) 111 | feedback 112 | ``` 113 | 114 | 115 | 116 | --- 117 | 118 | ```python 119 | from streamlit_feedback import streamlit_feedback 120 | feedback = streamlit_feedback(feedback_type="thumbs", align="flex-start") 121 | feedback 122 | ``` 123 | 124 | 125 | 126 | ## Contributing 127 | 128 | We welcome all contributions. To test & run the streamlit-feedback component locally, you will need to run both the frontend javascript component and the streamlit server. 129 | 130 | 1. Run the component frontend 131 | 132 | ``` 133 | cd streamlit_feedback/frontend && npm run start 134 | ``` 135 | 136 | 2. Run the server 137 | 138 | ```python 139 | streamlit run streamlit_feedback/__init__.py 140 | ``` 141 | 142 | See more details in [CONTRIBUTING.md](CONTRIBUTING.md). 143 | -------------------------------------------------------------------------------- /assets/faces.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/trubrics/streamlit-feedback/582194fefb536789ecf3dce86f59238e3aefc4ca/assets/faces.png -------------------------------------------------------------------------------- /assets/facesandtext.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/trubrics/streamlit-feedback/582194fefb536789ecf3dce86f59238e3aefc4ca/assets/facesandtext.png -------------------------------------------------------------------------------- /assets/thumbs.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/trubrics/streamlit-feedback/582194fefb536789ecf3dce86f59238e3aefc4ca/assets/thumbs.png -------------------------------------------------------------------------------- /assets/thumbsandtext.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/trubrics/streamlit-feedback/582194fefb536789ecf3dce86f59238e3aefc4ca/assets/thumbsandtext.png -------------------------------------------------------------------------------- /assets/thumbsstart.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/trubrics/streamlit-feedback/582194fefb536789ecf3dce86f59238e3aefc4ca/assets/thumbsstart.png -------------------------------------------------------------------------------- /pyproject.toml: -------------------------------------------------------------------------------- 1 | [tool.isort] 2 | profile = "black" 3 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | streamlit 2 | 3 | #to upload package 4 | twine 5 | setuptools 6 | wheel 7 | 8 | #linting 9 | black>=22.3.0 10 | isort>=5.10.1 11 | flake8>=4.0.1 12 | pre-commit>=2.19.0 13 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | from pathlib import Path 2 | 3 | import setuptools 4 | 5 | this_directory = Path(__file__).parent 6 | long_description = (this_directory / "README.md").read_text() 7 | 8 | setuptools.setup( 9 | name="streamlit-feedback", 10 | version="0.1.4", 11 | author="Jeff Kayne", 12 | author_email="jeff.kayne@trubrics.com", 13 | description=( 14 | "Streamlit component that allows you to collect user feedback in your apps" 15 | ), 16 | long_description=long_description, 17 | long_description_content_type="text/markdown", 18 | url="", 19 | packages=setuptools.find_packages(), 20 | include_package_data=True, 21 | classifiers=[ 22 | "Intended Audience :: Developers", 23 | "License :: OSI Approved :: MIT License", 24 | "Programming Language :: Python :: 3.7", 25 | "Programming Language :: Python :: 3.8", 26 | "Programming Language :: Python :: 3.9", 27 | "Programming Language :: Python :: 3.10", 28 | "Programming Language :: Python :: 3.11", 29 | ], 30 | python_requires=">=3.7", 31 | install_requires=[ 32 | # By definition, a Custom Component depends on Streamlit. 33 | # If your component has other Python dependencies, list 34 | # them here. 35 | "streamlit >= 0.63", 36 | ], 37 | ) 38 | -------------------------------------------------------------------------------- /streamlit_feedback/__init__.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | import streamlit as st 4 | import streamlit.components.v1 as components 5 | 6 | # Create a _RELEASE constant. We'll set this to False while we're developing 7 | # the component, and True when we're ready to package and distribute it. 8 | # (This is, of course, optional - there are innumerable ways to manage your 9 | # release process.) 10 | _RELEASE = False 11 | 12 | if not _RELEASE: 13 | _component_func = components.declare_component( 14 | "streamlit_feedback", url="http://localhost:3001" 15 | ) 16 | else: 17 | # When we're distributing a production version of the component, we'll 18 | # replace the `url` param with `path`, and point it to to the component's 19 | # build directory: 20 | parent_dir = os.path.dirname(os.path.abspath(__file__)) 21 | build_dir = os.path.join(parent_dir, "frontend/build") 22 | _component_func = components.declare_component("streamlit_feedback", path=build_dir) 23 | 24 | 25 | def streamlit_feedback( 26 | feedback_type, 27 | optional_text_label=None, 28 | max_text_length=None, 29 | disable_with_score=None, 30 | review_on_positive=True, 31 | on_submit=None, 32 | args=(), 33 | kwargs={}, 34 | align="flex-end", 35 | key=None, 36 | ): 37 | """Create a new instance of "streamlit_feedback". 38 | 39 | Parameters 40 | ---------- 41 | feedback_type: str 42 | The type of feedback; "thumbs" or "faces". 43 | optional_text_label: str or None 44 | An optional label to add as a placeholder to the textbox. 45 | If None, the "thumbs" or "faces" will not be accompanied by textual feedback. 46 | max_text_length: int or None 47 | Defaults to None. If set, enables the multi-line functionality and determines the maximum characters the textbox allows. Else, displays the default one-line textbox. 48 | disable_with_score: str 49 | An optional score to disable the component. Must be a "thumbs" emoji or a "faces" emoji. Can be used to pass state from one component to another. 50 | review_on_positive: bool 51 | Defaults to True. When set to False, it only asks for textual feedback in case of a negative review, i.e., "thumbs down" or less than the happiest "faces" emoji. 52 | on_submit: callable 53 | An optional callback invoked when feedback is submitted. This function must accept at least one argument, the feedback response dict, 54 | allowing you to save the feedback to a database for example. Additional arguments can be specified using `args` and `kwargs`. 55 | args: tuple 56 | Additional positional arguments to pass to `on_submit`. 57 | kwargs: dict 58 | Additional keyword arguments to pass to `on_submit`. 59 | align: str 60 | Where to align the feedback component; "flex-end", "center" or "flex-start". 61 | key: str or None 62 | An optional key that uniquely identifies this component. If this is 63 | None, and the component's arguments are changed, the component will 64 | be re-mounted in the Streamlit frontend and lose its current state. 65 | 66 | Returns 67 | ------- 68 | dict 69 | The user response, with the feedback_type, score and text fields. If on_submit returns a value, this value will be returned by the component. 70 | 71 | """ 72 | if feedback_type == "thumbs": 73 | possible_thumbs = ["👍", "👎"] 74 | if disable_with_score not in [None] + possible_thumbs: 75 | raise ValueError( 76 | f"disable_with_score='{disable_with_score}' not recognised. Please only" 77 | f" use {possible_thumbs} for feedback_type='thumbs'." 78 | ) 79 | elif feedback_type == "faces": 80 | possible_faces = ["😞", "🙁", "😐", "🙂", "😀"] 81 | if disable_with_score not in [None] + possible_faces: 82 | raise ValueError( 83 | f"disable_with_score='{disable_with_score}' not recognised. Please only" 84 | f" use {possible_faces} for feedback_type='faces'." 85 | ) 86 | else: 87 | raise NotImplementedError( 88 | f"feedback_type='{feedback_type}' not implemented. Please select either" 89 | " 'thumbs' or 'faces'." 90 | ) 91 | if align not in ["flex-end", "center", "flex-start"]: 92 | raise NotImplementedError( 93 | f"align='{align}' not implemented. Please select either 'flex-end'," 94 | " 'center' or 'flex-start'." 95 | ) 96 | if max_text_length and optional_text_label is None: 97 | raise NotImplementedError( 98 | "max_text_length requires optional_text_label to be set." 99 | ) 100 | 101 | if key is None: 102 | key = feedback_type 103 | 104 | if f"feedback_submitted_{key}" not in st.session_state: 105 | st.session_state[f"feedback_submitted_{key}"] = False 106 | 107 | component_value = _component_func( 108 | feedback_type=feedback_type, 109 | optional_text_label=optional_text_label, 110 | max_text_length=max_text_length, 111 | review_on_positive=review_on_positive, 112 | disable_with_score=disable_with_score, 113 | align=align, 114 | key=key, 115 | default=None, 116 | ) 117 | 118 | if st.session_state[f"feedback_submitted_{key}"] is True: 119 | return None 120 | else: 121 | if component_value: 122 | st.session_state[f"feedback_submitted_{key}"] = True 123 | if on_submit: 124 | feedback = on_submit(component_value, *args, **kwargs) 125 | if feedback: 126 | return feedback 127 | else: 128 | return component_value 129 | else: 130 | return component_value 131 | else: 132 | return None 133 | 134 | 135 | if not _RELEASE: 136 | 137 | # Added a try-except to make setting up the development environment for this project easier. 138 | try: 139 | from examples import ( 140 | bare_bones_app, 141 | basic_app, 142 | chatbot_thumbs_app, 143 | single_prediction_faces_app, 144 | streaming_chatbot, 145 | ) 146 | except: 147 | from examples import ( 148 | bare_bones_app, 149 | basic_app, 150 | chatbot_thumbs_app, 151 | single_prediction_faces_app, 152 | streaming_chatbot, 153 | ) 154 | 155 | page_names_to_funcs = { 156 | "Chatbot": chatbot_thumbs_app, 157 | "Streaming chatbot": streaming_chatbot, 158 | "Faces": single_prediction_faces_app, 159 | "Basic": basic_app, 160 | "Bare bones": bare_bones_app, 161 | } 162 | 163 | demo_name = st.sidebar.selectbox("Choose a demo", page_names_to_funcs.keys()) 164 | page_names_to_funcs[demo_name](streamlit_feedback=streamlit_feedback, debug=True) 165 | -------------------------------------------------------------------------------- /streamlit_feedback/examples.py: -------------------------------------------------------------------------------- 1 | import openai 2 | import streamlit as st 3 | 4 | 5 | def _submit_feedback(user_response, emoji=None): 6 | st.toast(f"Feedback submitted: {user_response}", icon=emoji) 7 | return user_response.update({"some metadata": 123}) 8 | 9 | 10 | def chatbot_thumbs_app(streamlit_feedback, debug=False): 11 | st.title("💬 Chatbot") 12 | 13 | with st.sidebar: 14 | openai_api_key = st.text_input( 15 | "OpenAI API Key", 16 | key="chatbot_api_key", 17 | type="password", 18 | value=st.secrets.get("OPENAI_API_KEY"), 19 | ) 20 | 21 | if "messages" not in st.session_state: 22 | st.session_state["messages"] = [ 23 | {"role": "assistant", "content": "How can I help you?"} 24 | ] 25 | 26 | messages = st.session_state.messages 27 | 28 | for n, msg in enumerate(messages): 29 | st.chat_message(msg["role"]).write(msg["content"]) 30 | 31 | if msg["role"] == "assistant" and n > 1: 32 | feedback_key = f"feedback_{int(n/2)}" 33 | 34 | if feedback_key not in st.session_state: 35 | st.session_state[feedback_key] = None 36 | 37 | streamlit_feedback( 38 | feedback_type="thumbs", 39 | optional_text_label="Please provide extra information", 40 | review_on_positive=False, 41 | on_submit=_submit_feedback, 42 | key=feedback_key, 43 | ) 44 | 45 | if prompt := st.chat_input(): 46 | messages.append({"role": "user", "content": prompt}) 47 | st.chat_message("user").write(prompt) 48 | 49 | if debug: 50 | st.session_state["response"] = "dummy response" 51 | else: 52 | if not openai_api_key: 53 | st.info("Please add your OpenAI API key to continue.") 54 | st.stop() 55 | else: 56 | openai.api_key = openai_api_key 57 | response = openai.ChatCompletion.create( 58 | model="gpt-3.5-turbo", messages=messages 59 | ) 60 | st.session_state["response"] = response.choices[0].message.content 61 | with st.chat_message("assistant"): 62 | messages.append( 63 | {"role": "assistant", "content": st.session_state["response"]} 64 | ) 65 | st.write(st.session_state["response"]) 66 | st.rerun() 67 | 68 | 69 | def single_prediction_faces_app(streamlit_feedback, debug=False): 70 | st.title("LLM User Feedback with Trubrics") 71 | 72 | if "response" not in st.session_state: 73 | st.session_state["response"] = "" 74 | if "feedback_key" not in st.session_state: 75 | st.session_state.feedback_key = 0 76 | 77 | with st.sidebar: 78 | openai_api_key = st.text_input( 79 | "OpenAI API Key", 80 | key="chatbot_api_key", 81 | type="password", 82 | value=st.secrets.get("OPENAI_API_KEY"), 83 | ) 84 | 85 | if not openai_api_key: 86 | st.info("Please add your OpenAI API key to continue.") 87 | st.stop() 88 | else: 89 | openai.api_key = openai_api_key 90 | 91 | prompt = st.text_area( 92 | label="Prompt", 93 | label_visibility="collapsed", 94 | placeholder="What would you like to know?", 95 | ) 96 | button = st.button(f"Ask text-davinci-002") 97 | 98 | if button: 99 | if debug: 100 | st.session_state["response"] = "dummy response: " + prompt.strip() 101 | else: 102 | st.session_state["response"] = openai.Completion.create( 103 | model="text-davinci-002", prompt=prompt, temperature=0.5, max_tokens=200 104 | ) 105 | st.session_state["response"] = ( 106 | st.session_state["response"].choices[0].text.replace("\n", "") 107 | ) 108 | st.session_state.feedback_key += 1 # overwrite feedback component 109 | 110 | if st.session_state["response"]: 111 | st.markdown(f"#### :violet[{st.session_state['response']}]") 112 | 113 | streamlit_feedback( 114 | feedback_type="faces", 115 | optional_text_label="Please provide extra information", 116 | review_on_positive=False, 117 | align="flex-start", 118 | on_submit=_submit_feedback, 119 | key=f"feedback_{st.session_state.feedback_key}", 120 | ) 121 | 122 | 123 | def basic_app(streamlit_feedback, debug): 124 | st.title("Component demo") 125 | 126 | if "feedback_key" not in st.session_state: 127 | st.session_state.feedback_key = 0 128 | 129 | st.button("Random button") 130 | 131 | if st.button("Refresh feedback component"): 132 | st.session_state.feedback_key += 1 # overwrite feedback component 133 | 134 | multiline = st.toggle("Multiline", value=False) 135 | 136 | if multiline: 137 | feedback = streamlit_feedback( 138 | feedback_type="faces", 139 | # on_submit=_submit_feedback, 140 | key=f"feedback_{st.session_state.feedback_key}", 141 | optional_text_label="Please provide some more information", 142 | max_text_length=500, 143 | args=["✅"], 144 | ) 145 | else: 146 | feedback = streamlit_feedback( 147 | feedback_type="faces", 148 | # on_submit=_submit_feedback, 149 | key=f"feedback_{st.session_state.feedback_key}", 150 | optional_text_label="Please provide some more information", 151 | args=["✅"], 152 | ) 153 | 154 | if feedback: 155 | st.write(":orange[Component output:]") 156 | st.write(feedback) 157 | 158 | 159 | def bare_bones_app(streamlit_feedback, debug): 160 | feedback = streamlit_feedback(feedback_type="faces", on_submit=_submit_feedback) 161 | 162 | if feedback: 163 | st.write(":orange[Component output:]") 164 | st.write(feedback) 165 | 166 | 167 | def streaming_chatbot(streamlit_feedback, debug): 168 | 169 | st.title("💬 Streaming Chatbot") 170 | 171 | openai.api_key = st.secrets["OPENAI_API_KEY"] 172 | 173 | if "openai_model" not in st.session_state: 174 | st.session_state["openai_model"] = "gpt-3.5-turbo" 175 | 176 | if "messages" not in st.session_state: 177 | st.session_state.messages = [] 178 | if "feedback_key" not in st.session_state: 179 | st.session_state.feedback_key = 0 180 | 181 | feedback_kwargs = { 182 | "feedback_type": "thumbs", 183 | "optional_text_label": "Please provide extra information", 184 | "on_submit": _submit_feedback, 185 | } 186 | 187 | for n, msg in enumerate(st.session_state.messages): 188 | st.chat_message(msg["role"]).write(msg["content"]) 189 | 190 | if msg["role"] == "assistant" and n > 1: 191 | feedback_key = f"feedback_{int(n/2)}" 192 | 193 | if feedback_key not in st.session_state: 194 | st.session_state[feedback_key] = None 195 | 196 | disable_with_score = ( 197 | st.session_state[feedback_key].get("score") 198 | if st.session_state[feedback_key] 199 | else None 200 | ) 201 | streamlit_feedback( 202 | **feedback_kwargs, 203 | key=feedback_key, 204 | disable_with_score=disable_with_score, 205 | ) 206 | 207 | if prompt := st.chat_input("What is up?"): 208 | st.session_state.messages.append({"role": "user", "content": prompt}) 209 | with st.chat_message("user"): 210 | st.markdown(prompt) 211 | with st.chat_message("assistant"): 212 | message_placeholder = st.empty() 213 | full_response = "" 214 | 215 | for response in openai.ChatCompletion.create( 216 | model=st.session_state["openai_model"], 217 | messages=[ 218 | {"role": m["role"], "content": m["content"]} 219 | for m in st.session_state.messages 220 | ], 221 | stream=True, 222 | ): 223 | full_response += response.choices[0].delta.get("content", "") 224 | message_placeholder.markdown(full_response + "▌") 225 | message_placeholder.markdown(full_response) 226 | st.session_state.messages.append( 227 | {"role": "assistant", "content": full_response} 228 | ) 229 | streamlit_feedback( 230 | **feedback_kwargs, key=f"feedback_{int(len(st.session_state.messages)/2)}" 231 | ) 232 | -------------------------------------------------------------------------------- /streamlit_feedback/frontend/.env: -------------------------------------------------------------------------------- 1 | # Run the component's dev server on :3001 2 | # (The Streamlit dev server already runs on :3000) 3 | PORT=3001 4 | 5 | # Don't automatically open the web browser on `npm run start`. 6 | BROWSER=none 7 | -------------------------------------------------------------------------------- /streamlit_feedback/frontend/.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "endOfLine": "lf", 3 | "semi": false, 4 | "trailingComma": "es5" 5 | } 6 | -------------------------------------------------------------------------------- /streamlit_feedback/frontend/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "streamlit_feedback", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "@emotion/react": "^11.11.1", 7 | "@emotion/styled": "^11.11.0", 8 | "@mui/icons-material": "^5.14.3", 9 | "@mui/material": "^5.14.3", 10 | "react": "^17.0.0", 11 | "react-dom": "^17.0.0", 12 | "streamlit-component-lib": "^2.0.0" 13 | }, 14 | "scripts": { 15 | "start": "react-scripts start", 16 | "build": "react-scripts build", 17 | "test": "react-scripts test", 18 | "eject": "react-scripts eject" 19 | }, 20 | "eslintConfig": { 21 | "extends": "react-app" 22 | }, 23 | "browserslist": { 24 | "production": [ 25 | ">0.2%", 26 | "not dead", 27 | "not op_mini all" 28 | ], 29 | "development": [ 30 | "last 1 chrome version", 31 | "last 1 firefox version", 32 | "last 1 safari version" 33 | ] 34 | }, 35 | "homepage": ".", 36 | "devDependencies": { 37 | "@types/node": "^12.0.0", 38 | "@types/react": "^18.2.18", 39 | "@types/react-dom": "^16.9.0", 40 | "react-scripts": "^5.0.1", 41 | "typescript": "^4.2.0" 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /streamlit_feedback/frontend/public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Streamlit Component 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 |
14 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /streamlit_feedback/frontend/src/FacesFeedback.js: -------------------------------------------------------------------------------- 1 | import React, { useState, useEffect } from "react"; 2 | import SentimentVeryDissatisfiedIcon from '@mui/icons-material/SentimentVeryDissatisfied'; 3 | import SentimentDissatisfiedIcon from '@mui/icons-material/SentimentDissatisfied'; 4 | import SentimentNeutralIcon from '@mui/icons-material/SentimentNeutral'; 5 | import SentimentSatisfiedIcon from '@mui/icons-material/SentimentSatisfied'; 6 | import SentimentSatisfiedAltIcon from '@mui/icons-material/SentimentSatisfiedAlt'; 7 | import Stack from '@mui/material/Stack'; 8 | 9 | const colors = { 10 | grey: "#c7d1d3", 11 | "😀": "#4caf50", 12 | "🙂": "#6fbf73", 13 | "😐": "#ff9800", 14 | "🙁": "#f6685e", 15 | "😞": "#f44336" 16 | } 17 | 18 | export function FacesFeedback(props) { 19 | const [faceScore, setFaceScore] = useState(null); 20 | 21 | useEffect(() => { 22 | if (props.disableWithScore){ 23 | setFaceScore(props.disableWithScore); 24 | } 25 | }, [props.disableWithScore]) 26 | 27 | const handleFaceClick = (score) => { 28 | setFaceScore(score); 29 | props.submitFeedback(score, null); 30 | }; 31 | 32 | const selectColor = (score) => { 33 | if (faceScore) { 34 | if (score === faceScore) { 35 | return colors[score] 36 | } else { 37 | return "transparent" 38 | } 39 | } else { 40 | return colors["grey"] 41 | } 42 | } 43 | 44 | const selectHoverColor = (score) => { 45 | if (faceScore) { 46 | if (score === faceScore) { 47 | return colors[score] 48 | } else { 49 | return "transparent" 50 | } 51 | } else { 52 | return colors[score] 53 | } 54 | } 55 | 56 | return ( 57 | 58 | faceScore !== null ? {} : handleFaceClick("😞")} 67 | /> 68 | faceScore !== null ? {} : handleFaceClick("🙁")} 77 | /> 78 | faceScore !== null ? {} : handleFaceClick("😐")} 87 | /> 88 | faceScore !== null ? {} : handleFaceClick("🙂")} 97 | /> 98 | faceScore !== null ? {} : handleFaceClick("😀")} 107 | /> 108 | 109 | ) 110 | } 111 | -------------------------------------------------------------------------------- /streamlit_feedback/frontend/src/FacesWithQualiFeedback.js: -------------------------------------------------------------------------------- 1 | import React, { useState, useEffect } from "react"; 2 | import SentimentVeryDissatisfiedIcon from '@mui/icons-material/SentimentVeryDissatisfied'; 3 | import SentimentDissatisfiedIcon from '@mui/icons-material/SentimentDissatisfied'; 4 | import SentimentNeutralIcon from '@mui/icons-material/SentimentNeutral'; 5 | import SentimentSatisfiedIcon from '@mui/icons-material/SentimentSatisfied'; 6 | import SentimentSatisfiedAltIcon from '@mui/icons-material/SentimentSatisfiedAlt'; 7 | import InputBase from '@mui/material/InputBase'; 8 | import Button from '@mui/material/Button'; 9 | import Stack from '@mui/material/Stack'; 10 | import { Box } from "@mui/material"; 11 | 12 | import { styled } from '@mui/material/styles'; 13 | import TextField from "@mui/material/TextField"; 14 | 15 | const colors = { 16 | grey: "#c7d1d3", 17 | "😀": "#4caf50", 18 | "🙂": "#6fbf73", 19 | "😐": "#ff9800", 20 | "🙁": "#f6685e", 21 | "😞": "#f44336" 22 | } 23 | 24 | const TextFieldcolors = { 25 | "😀": "success", 26 | "🙂": "success", 27 | "😐": "warning", 28 | "🙁": "error", 29 | "😞": "error" 30 | } 31 | 32 | const StyledCustomInput = styled(InputBase)( 33 | ({ color }) => ` 34 | width: 60vw; 35 | font-family: sans-serif; 36 | font-size: 0.875rem; 37 | font-weight: 400; 38 | padding: 0px 12px; 39 | border-radius: 8px; 40 | color: ${color}; 41 | border: 1px solid ${color}; 42 | background: transparent; 43 | ` 44 | ); 45 | 46 | const StyledTextField = styled(TextField)( 47 | ({ color }) => ` 48 | width: 60vw; 49 | font-family: sans-serif; 50 | font-size: 0.875rem; 51 | font-weight: 400; 52 | padding: 0px 12px; 53 | border-radius: 8px; 54 | color: ${color}; 55 | border: 1px solid ${color}; 56 | background: transparent; 57 | ` 58 | ); 59 | 60 | export function FacesWithQualiFeedback(props) { 61 | const [submitted, setSubmitted] = useState(false); 62 | const [inputText, setInputText] = useState(null); 63 | const [faceScore, setFaceScore] = useState(null); 64 | 65 | useEffect(() => { 66 | if (props.disableWithScore) { 67 | setSubmitted(true); 68 | setFaceScore(props.disableWithScore); 69 | } 70 | }, [props.disableWithScore]) 71 | 72 | const handleFaceClick = (score) => { 73 | if (score === faceScore) { 74 | setFaceScore(null); 75 | } else { 76 | setFaceScore(score); 77 | } 78 | }; 79 | 80 | const selectColor = (score) => { 81 | if (faceScore) { 82 | if (score === faceScore) { 83 | return colors[score] 84 | } else { 85 | if (submitted) { 86 | return "transparent" 87 | } else { 88 | return colors["grey"] 89 | } 90 | } 91 | } else { 92 | return colors["grey"] 93 | } 94 | } 95 | 96 | const selectHoverColor = (score) => { 97 | if (faceScore) { 98 | if (score === faceScore) { 99 | return colors[score] 100 | } else { 101 | if (submitted) { 102 | return "transparent" 103 | } else { 104 | return colors[score] 105 | } 106 | } 107 | } else { 108 | return colors[score] 109 | } 110 | } 111 | 112 | const handleTextInput = (text) => { 113 | setInputText(text.currentTarget.value); 114 | }; 115 | 116 | const handleSubmission = () => { 117 | setSubmitted(true); 118 | props.submitFeedback(faceScore, inputText); 119 | }; 120 | 121 | 122 | if (props.maxTextLength != null) { 123 | return ( 124 | 125 | 126 | submitted ? {} : handleFaceClick("😞")} 136 | /> 137 | submitted ? {} : handleFaceClick("🙁")} 147 | /> 148 | submitted ? {} : handleFaceClick("😐")} 158 | /> 159 | submitted ? {} : handleFaceClick("🙂")} 169 | /> 170 | submitted ? {} : handleFaceClick("😀")} 180 | /> 181 | {submitted === false && faceScore !== null ? : null} 182 | {submitted === false && faceScore !== null && props.reviewOnPositive === true ? : null} 183 | {submitted === false && faceScore !== "😀" && !props.reviewOnPositive ? : null} 184 | {submitted === false && faceScore !== null ? : null} 185 | 186 | 187 | ) 188 | } 189 | else { 190 | 191 | return ( 192 | 193 | 194 | submitted ? {} : handleFaceClick("😞")} 204 | /> 205 | submitted ? {} : handleFaceClick("🙁")} 215 | /> 216 | submitted ? {} : handleFaceClick("😐")} 226 | /> 227 | submitted ? {} : handleFaceClick("🙂")} 237 | /> 238 | submitted ? {} : handleFaceClick("😀")} 248 | /> 249 | {submitted === false && faceScore !== null && props.reviewOnPositive === true ? : null} 250 | {submitted === false && faceScore !== "😀" && !props.reviewOnPositive? : null} 251 | {/* {submitted === false && faceScore !== null ? : null} */} 252 | {submitted === false && faceScore !== null ? : null} 253 | 254 | 255 | ) 256 | } 257 | } -------------------------------------------------------------------------------- /streamlit_feedback/frontend/src/MyComponent.js: -------------------------------------------------------------------------------- 1 | import { 2 | StreamlitComponentBase, 3 | withStreamlitConnection, 4 | } from "streamlit-component-lib" 5 | import React from "react" 6 | import { Feedback } from "./feedback" 7 | 8 | 9 | class MyComponent extends StreamlitComponentBase { 10 | 11 | render() { 12 | return ( 13 |
14 | 23 |
24 | ) 25 | } 26 | } 27 | 28 | export default withStreamlitConnection(MyComponent) 29 | -------------------------------------------------------------------------------- /streamlit_feedback/frontend/src/ThumbsFeedback.js: -------------------------------------------------------------------------------- 1 | import React, { useState, useEffect } from "react"; 2 | import ThumbDownOffAltIcon from '@mui/icons-material/ThumbDownOffAlt'; 3 | import ThumbUpOffAltIcon from '@mui/icons-material/ThumbUpOffAlt'; 4 | import Stack from '@mui/material/Stack'; 5 | 6 | const colors = { 7 | colorGrey: "#c7d1d3", 8 | colorUp: "#4caf50", 9 | colorDown: "#f44336" 10 | } 11 | 12 | export function ThumbsFeedback(props) { 13 | const [thumbScore, setThumbScore] = useState(null); 14 | 15 | useEffect(() => { 16 | if (props.disableWithScore){ 17 | setThumbScore(props.disableWithScore); 18 | } 19 | }, [props.disableWithScore]) 20 | 21 | let thumbUpColor = colors["colorGrey"]; 22 | let thumbHoverUpColor = colors["colorUp"]; 23 | let thumbDownColor = colors["colorGrey"]; 24 | let thumbHoverDownColor = colors["colorDown"]; 25 | if (thumbScore) { 26 | thumbUpColor = thumbScore === "👍" ? colors["colorUp"] : "transparent" 27 | thumbHoverUpColor = thumbScore === "👍" ? colors["colorUp"] : "transparent" 28 | thumbDownColor = thumbScore === "👎" ? colors["colorDown"] : "transparent" 29 | thumbHoverDownColor = thumbScore === "👎" ? colors["colorDown"] : "transparent" 30 | } 31 | 32 | const handleThumbClick = (score) => { 33 | setThumbScore(score); 34 | props.submitFeedback(score, null); 35 | }; 36 | 37 | return ( 38 | 39 | thumbScore !== null ? {} : handleThumbClick("👍")} 48 | /> 49 | thumbScore !== null ? {} : handleThumbClick("👎")} 58 | /> 59 | 60 | ) 61 | } 62 | -------------------------------------------------------------------------------- /streamlit_feedback/frontend/src/ThumbsWithQualiFeedback.js: -------------------------------------------------------------------------------- 1 | import React, { useState, useEffect } from "react"; 2 | import ThumbDownOffAltIcon from '@mui/icons-material/ThumbDownOffAlt'; 3 | import ThumbUpOffAltIcon from '@mui/icons-material/ThumbUpOffAlt'; 4 | import InputBase from '@mui/material/InputBase'; 5 | import Button from '@mui/material/Button'; 6 | import Stack from '@mui/material/Stack'; 7 | import { Box } from "@mui/material"; 8 | 9 | import { styled } from '@mui/material/styles'; 10 | import TextField from "@mui/material/TextField"; 11 | 12 | const colors = { 13 | colorGrey: "#c7d1d3", 14 | colorUp: "#4caf50", 15 | colorDown: "#f44336" 16 | } 17 | 18 | const TextFieldcolors = { 19 | colorUp: "success", 20 | colorDown: "error" 21 | } 22 | 23 | const StyledCustomInput = styled(InputBase)( 24 | ({ color }) => ` 25 | width: 70vw; 26 | font-family: sans-serif; 27 | font-size: 0.875rem; 28 | font-weight: 400; 29 | padding: 0px 12px; 30 | border-radius: 8px; 31 | color: ${color}; 32 | border: 1px solid ${color}; 33 | background: transparent; 34 | ` 35 | ); 36 | 37 | const StyledTextField = styled(TextField)( 38 | ({ color }) => ` 39 | width: 60vw; 40 | font-family: sans-serif; 41 | font-size: 0.875rem; 42 | font-weight: 400; 43 | padding: 0px 12px; 44 | border-radius: 8px; 45 | color: ${color}; 46 | border: 1px solid ${color}; 47 | background: transparent; 48 | ` 49 | ); 50 | 51 | export function ThumbsWithQualiFeedback(props) { 52 | const [thumbScore, setThumbScore] = useState(null); 53 | const [submitted, setSubmitted] = useState(false); 54 | const [inputText, setInputText] = useState(null); 55 | 56 | useEffect(() => { 57 | if (props.disableWithScore) { 58 | setSubmitted(true); 59 | setThumbScore(props.disableWithScore); 60 | } 61 | }, [props.disableWithScore]) 62 | 63 | let thumbUpColor; 64 | let thumbHoverUpColor; 65 | let thumbDownColor; 66 | let thumbHoverDownColor; 67 | if (thumbScore === "👍") { 68 | thumbUpColor = colors["colorUp"] 69 | thumbHoverUpColor = colors["colorUp"] 70 | thumbDownColor = submitted ? "transparent" : colors["colorGrey"] 71 | thumbHoverDownColor = submitted ? "transparent" : colors["colorDown"] 72 | } else if (thumbScore === "👎") { 73 | thumbUpColor = submitted ? "transparent" : colors["colorGrey"] 74 | thumbHoverUpColor = submitted ? "transparent" : colors["colorUp"] 75 | thumbDownColor = colors["colorDown"] 76 | thumbHoverDownColor = colors["colorDown"] 77 | } else { 78 | thumbUpColor = colors["colorGrey"] 79 | thumbHoverUpColor = colors["colorUp"] 80 | thumbDownColor = colors["colorGrey"] 81 | thumbHoverDownColor = colors["colorDown"] 82 | } 83 | 84 | if (submitted) { 85 | thumbUpColor = thumbScore === "👍" ? colors["colorUp"] : "transparent" 86 | thumbHoverUpColor = thumbScore === "👍" ? colors["colorUp"] : "transparent" 87 | thumbDownColor = thumbScore === "👎" ? colors["colorDown"] : "transparent" 88 | thumbHoverDownColor = thumbScore === "👎" ? colors["colorDown"] : "transparent" 89 | } 90 | 91 | 92 | const handleThumbClick = (score) => { 93 | if (score === thumbScore) { 94 | setThumbScore(null); 95 | } else { 96 | setThumbScore(score); 97 | } 98 | }; 99 | 100 | const handleTextInput = (text) => { 101 | setInputText(text.currentTarget.value); 102 | }; 103 | 104 | const handleSubmission = () => { 105 | props.submitFeedback(thumbScore, inputText); 106 | setSubmitted(true); 107 | }; 108 | 109 | if (props.maxTextLength != null) { 110 | return ( 111 | 112 | 113 | submitted ? {} : handleThumbClick("👍")} 123 | /> 124 | submitted ? {} : handleThumbClick("👎")} 134 | /> 135 | {submitted === false && thumbScore !== null && props.reviewOnPositive === true? : null} 136 | {submitted === false && thumbScore === "👎" && !props.reviewOnPositive ? : null} 137 | {submitted === false && thumbScore !== null ? : null} 138 | {submitted === false && thumbScore !== null ? : null} 139 | 140 | 141 | ) 142 | } 143 | else { 144 | return ( 145 | 146 | 147 | submitted ? {} : handleThumbClick("👍")} 157 | /> 158 | submitted ? {} : handleThumbClick("👎")} 168 | /> 169 | {submitted === false && thumbScore !== null && props.reviewOnPositive === true ? : null} 170 | {submitted === false && thumbScore === "👎" && !props.reviewOnPositive ? : null} 171 | {/* {submitted === false && thumbScore !== null ? : null} */} 172 | {submitted === false && thumbScore !== null ? : null} 173 | 174 | 175 | ) 176 | } 177 | } 178 | -------------------------------------------------------------------------------- /streamlit_feedback/frontend/src/feedback.js: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import { ThumbsFeedback } from "./ThumbsFeedback"; 3 | import { ThumbsWithQualiFeedback } from "./ThumbsWithQualiFeedback"; 4 | import { FacesFeedback } from "./FacesFeedback"; 5 | import { FacesWithQualiFeedback } from "./FacesWithQualiFeedback"; 6 | import { Streamlit } from "streamlit-component-lib" 7 | 8 | export function Feedback(props) { 9 | const submitFeedback = (score, text) => { 10 | Streamlit.setComponentValue({type: props.feedbackType, score, text}); 11 | }; 12 | 13 | if (props.feedbackType === "thumbs" && props.optionalTextLabel === null) { 14 | return () 15 | } else if (props.feedbackType === "thumbs" && props.optionalTextLabel !== null && props.maxTextLength === null) { 16 | return () 17 | } else if (props.feedbackType === "thumbs" && props.optionalTextLabel !== null && props.maxTextLength) { 18 | return () 19 | } else if (props.feedbackType === "faces" && props.optionalTextLabel === null && props.maxTextLength === null) { 20 | return () 21 | } else if (props.feedbackType === "faces" && props.optionalTextLabel !== null && props.maxTextLength === null) { 22 | return () 23 | } else if (props.feedbackType === "faces" && props.optionalTextLabel !== null && props.maxTextLength) { 24 | return () 25 | } else if (props.feedbackType === "textbox") { 26 | return (
) 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /streamlit_feedback/frontend/src/index.js: -------------------------------------------------------------------------------- 1 | import React from "react" 2 | import ReactDOM from "react-dom" 3 | import MyComponent from "./MyComponent" 4 | 5 | ReactDOM.render( 6 | 7 | 8 | , 9 | document.getElementById("root") 10 | ) 11 | -------------------------------------------------------------------------------- /streamlit_feedback/frontend/src/react-app-env.d.ts: -------------------------------------------------------------------------------- 1 | /// 2 | -------------------------------------------------------------------------------- /streamlit_feedback/frontend/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "es5", 4 | "lib": ["dom", "dom.iterable", "esnext"], 5 | "allowJs": true, 6 | "skipLibCheck": true, 7 | "esModuleInterop": true, 8 | "allowSyntheticDefaultImports": true, 9 | "strict": true, 10 | "forceConsistentCasingInFileNames": true, 11 | "module": "esnext", 12 | "moduleResolution": "node", 13 | "resolveJsonModule": true, 14 | "isolatedModules": true, 15 | "noEmit": true, 16 | "jsx": "react" 17 | }, 18 | "include": ["src"] 19 | } 20 | --------------------------------------------------------------------------------