Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open in GitHub sticky button #96

Open
arnaudmiribel opened this issue Nov 25, 2022 · 3 comments
Open

Open in GitHub sticky button #96

arnaudmiribel opened this issue Nov 25, 2022 · 3 comments
Assignees

Comments

@arnaudmiribel
Copy link
Owner

arnaudmiribel commented Nov 25, 2022

Would be great to have an extra that makes an app play well with GitHub, and:

  • adds a sticky github button to reach the source code of an app Open in GitHub sorta
  • optionally adds the . trigger to open github.dev, too

API:

  • add_sticky_github_button(label="Open in GitHub", owner="arnaudmiribel", repository="streamlit-extras")
  • add_sticky_github_button(label="Open in GitHub", repository="arnaudmiribel/streamlit-extras")
@arnaudmiribel
Copy link
Owner Author

arnaudmiribel commented Nov 25, 2022

<div class="stActionButton">
    <a href="http://www.example.com" style="text-decoration:none; color:inherit; background:inherit;" target="_blank" rel="noopener noreferrer"><button class="css-9s5bis edgvbvh3"><div class="css-1wbqy5l e10z71041"><img src="https://cdn-icons-png.flaticon.com/512/25/25231.png" style="width: 1em; height: 1em; vertical-align: -0.15em; border-radius: 3px; margin-right: 0em;"> 
<span>arnaudmiribel/markdownlit</span></div></button></a></div>
  • some JS to add it to the right place (first child in <div data-testid="stToolbar" ...> </div>)

Example:
image

@arnaudmiribel
Copy link
Owner Author

arnaudmiribel commented Nov 25, 2022

Could also just be generalized for any icon/label/link actually, and calling it twice just adds two buttons:

add_button_to_toolbar(icon="github", label="Open in GitHub", url="foo")
add_button_to_toolbar(icon="https://link_to_twitter_logo.png", label="arnaudmiribel", url="foo")

image

@arnaudmiribel
Copy link
Owner Author

arnaudmiribel commented Dec 9, 2022

Unsuccessful trial so far:

from streamlit.components.v1 import html
html(
    """
<script>

var newButtonHtmlString = `
<div class="stActionButton">
    <button class="css-9s5bis edgvbvh3"><div class="css-1wbqy5l e10z71041"><img src="https://cdn-icons-png.flaticon.com/512/25/25231.png" style="width: 1em; height: 1em; vertical-align: -0.15em; border-radius: 3px; margin-right: 0em;">
<span>🐙 Open code</span></div></button></a></div>
`;

var newButton = new DOMParser().parseFromString(newButtonHtmlString, "text/html");
var toolBar = document.querySelector('div[data-testid="stToolbar"]');
toolBar.insertBefore(newButton.body.firstChild, toolBar.firstChild);
</script>
"""
)

While this JS works in the console, it doesn't in the Streamlit app. Probably need to wait for the DOM to load before throwing it...

Actually even with

window.addEventListener('load', function () {
    var toolBar = document.querySelector('div[data-testid="stToolbar"]');
    toolBar.insertBefore(newButton.body.firstChild, toolBar.firstChild);
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant