-
Notifications
You must be signed in to change notification settings - Fork 868
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[custom tool] Provide guidance for configuring tool icon (#616)
# Description Provide guidance for configuring tool icon. # All Promptflow Contribution checklist: - [X] **The pull request does not introduce [breaking changes].** - [ ] **CHANGELOG is updated for new features, bug fixes or other significant changes.** - [X] **I have read the [contribution guidelines](../CONTRIBUTING.md).** - [ ] **Create an issue and link to the pull request to get dedicated review from promptflow team. Learn more: [suggested workflow](../CONTRIBUTING.md#suggested-workflow).** ## General Guidelines and Best Practices - [X] Title of the pull request is clear and informative. - [X] There are a small number of commits, each of which have an informative message. This means that previously merged commits do not appear in the history of the PR. For more information on cleaning up the commits in your PR, [see this page](https://github.com/Azure/azure-powershell/blob/master/documentation/development-docs/cleaning-up-commits.md). ### Testing Guidelines - [ ] Pull request includes test coverage for the included changes.
- Loading branch information
1 parent
65f8537
commit 037f627
Showing
11 changed files
with
141 additions
and
8 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 |
---|---|---|
|
@@ -126,7 +126,8 @@ | |
"tcsetattr", | ||
"pysqlite", | ||
"AADSTS700082", | ||
"levelno" | ||
"levelno", | ||
"LANCZOS" | ||
], | ||
"allowCompoundWords": true | ||
} |
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
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
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,58 @@ | ||
# Add a Tool Icon | ||
A tool icon serves as a graphical representation of your tool in the user interface (UI). Follow this guidance to add a custom tool icon when developing your own tool package. | ||
|
||
Adding a custom tool icon is optional. If you do not provide one, the system uses a default icon. | ||
|
||
## Prerequisites | ||
|
||
- Create a tool package as described in [Create and Use Tool Package](create-and-use-tool-package.md). | ||
- Prepare a custom tool icon image. The image should meet following requirements: | ||
- The image should be in `PNG` or `JPG` format. | ||
- 16x16 pixel image to avoid distortion from resizing. | ||
- Avoid complex images with much detail or contrast as they may not resize well. | ||
You could see for example. | ||
- Install dependencies of generating icon data URI: | ||
|
||
``` | ||
pip install pillow | ||
``` | ||
## Generate a tool icon data URI | ||
Run below command under the root folder to generate a data URI for your custom tool icon. Make sure the output file has an `.html` extension, this makes it easier to check the image's data URI: | ||
``` | ||
python <path-to-scripts>\tool\convert_image_to_data_url.py --image-path <image_input_path> -o <html_output_path> | ||
``` | ||
For example: | ||
``` | ||
python D:\proj\github\promptflow\scripts\tool\convert_image_to_data_url.py --image-path D:\proj\github\promptflow\examples\tools\tool-package-quickstart\my_tool_package\icons\custom-tool-icon.png -o output.html | ||
``` | ||
The content of `output.html` looks like the following, and you can open it in a web browser to see the result. | ||
```html | ||
<html> | ||
<body> | ||
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAACq0lEQVR4nKWTPWxTVxiGn3N/bF/fGyd2YxsQ5LdSmoqGgKJMEQNZYGBFkVhY2JGYmFG3ioWZpWukqqpaqaVKq6pFIAECAkSxMA6RHZP4Jnbs+O/6xvdjcCLKT6ee7ZwjvfrOc55XiYjwP5bx740IZDf3+PVZicdrVeK2yfzJJBem02iaQv1XQCCCCNz+Lce91R1mRvtYd5uEDIfl9SqpWIjZLxOIgPooRQtE0JQiU6xx91mJfNkjZoX47vIkM2Nx6l6Xmz9kWHywgVIQBB++WImI1Nv7fP/XOqah0fKFdH8YQwXcf1Vh6WWZTrfLaDLK4rVZrJD+wSQGwJrbQtc0rs4PAXDr5xy/PHW5NJsmGQuhNI0/XrisFmtMjwxwOLVCYXTaTdq+kHagXq0iAo4phE2dn564XD8/zLlTRwn8gK1dQaHQtfcgDDMcwQo1Wc43mEp16YpibdsjEKHdEX5/8YZEpIhjCckBi9a+ibfvETEsIobdY1Bp+Pz4cAvP67C522IsbeN1A0zd5r77LWF7hebe1xxJrzKRmON56W/OHDnHwskbaCIQt03OTsbJljxeuz4rxSYXp2JcmYszedQhrNscj/ehJIKuQpiaBegHEFVPoOHBKOPpKMXdNtlSmzt/bpC0XTb9LxgcmGDq2CT5mpC0hhiO1UhGe8ANBYgCQ1dcnR9iJGnxT6ZMrtLmbV1H78/QrD0nagQ82ljCP+HzqLBEsB8wP7bQ+8ZDpoauuHA6xfnpFA3Px4mY3M2cJbeTZjTxFQYm44lv0MRkPDH1aRcOtdaUwon0rgrbBdbd10S1AXJbWRxzkLXNLDEz1VP54wDtQLHuQUl36xUKpTzl6jYFN89OdYeCm6eyV3mv8mdKxuFxueHS8PawTJuW3yAacmh26jiRfhL2IO8AhSUo7nmFnjUAAAAASUVORK5CYII=" alt="My Image"> | ||
</body> | ||
</html> | ||
``` | ||
|
||
## Use the tool icon data URI in the tool YAML | ||
In the auto-generated tool YAML file, customize the tool icon by adding the data URI: | ||
```yaml | ||
hello_world.tools.hello_world_tool.get_greeting_message | ||
function: get_greeting_message | ||
inputs: | ||
connection: | ||
type: | ||
- CustomConnection | ||
input_text: | ||
type: | ||
- string | ||
module: hello_world.tools.hello_world_tool | ||
name: Hello World Tool | ||
description: This is hello world tool | ||
type: python | ||
icon: data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAACq0lEQVR4nKWTPWxTVxiGn3N/bF/fGyd2YxsQ5LdSmoqGgKJMEQNZYGBFkVhY2JGYmFG3ioWZpWukqqpaqaVKq6pFIAECAkSxMA6RHZP4Jnbs+O/6xvdjcCLKT6ee7ZwjvfrOc55XiYjwP5bx740IZDf3+PVZicdrVeK2yfzJJBem02iaQv1XQCCCCNz+Lce91R1mRvtYd5uEDIfl9SqpWIjZLxOIgPooRQtE0JQiU6xx91mJfNkjZoX47vIkM2Nx6l6Xmz9kWHywgVIQBB++WImI1Nv7fP/XOqah0fKFdH8YQwXcf1Vh6WWZTrfLaDLK4rVZrJD+wSQGwJrbQtc0rs4PAXDr5xy/PHW5NJsmGQuhNI0/XrisFmtMjwxwOLVCYXTaTdq+kHagXq0iAo4phE2dn564XD8/zLlTRwn8gK1dQaHQtfcgDDMcwQo1Wc43mEp16YpibdsjEKHdEX5/8YZEpIhjCckBi9a+ibfvETEsIobdY1Bp+Pz4cAvP67C522IsbeN1A0zd5r77LWF7hebe1xxJrzKRmON56W/OHDnHwskbaCIQt03OTsbJljxeuz4rxSYXp2JcmYszedQhrNscj/ehJIKuQpiaBegHEFVPoOHBKOPpKMXdNtlSmzt/bpC0XTb9LxgcmGDq2CT5mpC0hhiO1UhGe8ANBYgCQ1dcnR9iJGnxT6ZMrtLmbV1H78/QrD0nagQ82ljCP+HzqLBEsB8wP7bQ+8ZDpoauuHA6xfnpFA3Px4mY3M2cJbeTZjTxFQYm44lv0MRkPDH1aRcOtdaUwon0rgrbBdbd10S1AXJbWRxzkLXNLDEz1VP54wDtQLHuQUl36xUKpTzl6jYFN89OdYeCm6eyV3mv8mdKxuFxueHS8PawTJuW3yAacmh26jiRfhL2IO8AhSUo7nmFnjUAAAAASUVORK5CYII= | ||
``` | ||
## Verify the tool icon in VS Code extension | ||
Follow [steps](create-and-use-tool-package.md#use-your-tool-from-vscode-extension) to use your tool from VS Code extension. Your tool displays with the custom icon: | ||
![custom-tool-with-icon-in-extension](../../media/contributing/custom-tool-with-icon-in-extension.png) |
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
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,10 @@ | ||
# Develop a tool | ||
We provide guides on how to develop a tool and use it. | ||
|
||
```{toctree} | ||
:maxdepth: 1 | ||
:hidden: | ||
create-and-use-tool-package | ||
add-a-tool-icon | ||
``` |
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+692 Bytes
examples/tools/tool-package-quickstart/my_tool_package/icons/custom-tool-icon.png
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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import argparse | ||
import base64 | ||
import os | ||
import io | ||
from PIL import Image | ||
|
||
|
||
def get_image_size(image_path): | ||
with Image.open(image_path) as img: | ||
width, height = img.size | ||
return width, height | ||
|
||
|
||
def get_image_storage_size(image_path): | ||
file_size_bytes = os.path.getsize(image_path) | ||
file_size_mb = file_size_bytes / (1024 * 1024) | ||
return file_size_mb | ||
|
||
|
||
def image_to_data_url(image_path): | ||
with open(image_path, "rb") as image_file: | ||
# Create a BytesIO object from the image file | ||
image_data = io.BytesIO(image_file.read()) | ||
|
||
# Open the image and resize it | ||
img = Image.open(image_data) | ||
if img.size != (16, 16): | ||
img = img.resize((16, 16), Image.Resampling.LANCZOS) | ||
|
||
# Save the resized image to a data URL | ||
buffered = io.BytesIO() | ||
img.save(buffered, format="PNG") | ||
img_str = base64.b64encode(buffered.getvalue()) | ||
data_url = 'data:image/png;base64,' + img_str.decode('utf-8') | ||
|
||
return data_url | ||
|
||
|
||
def create_html_file(data_uri, output_path): | ||
html_content = '<html>\n<body>\n<img src="{}" alt="My Image">\n</body>\n</html>'.format(data_uri) | ||
|
||
with open(output_path, 'w') as file: | ||
file.write(html_content) | ||
|
||
|
||
if __name__ == "__main__": | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument( | ||
"--image-path", | ||
type=str, | ||
required=True, | ||
help="Your image input path", | ||
) | ||
parser.add_argument( | ||
"--output", | ||
"-o", | ||
type=str, | ||
required=True, | ||
help="Your image output path", | ||
) | ||
args = parser.parse_args() | ||
data_url = image_to_data_url(args.image_path) | ||
print("Your image data uri: \n{}".format(data_url)) | ||
create_html_file(data_url, args.output) |
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
037f627
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ajarin saya dong