Skip to content

core: ✨ add new package space-header #752

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

Merged
merged 7 commits into from
Jun 14, 2024
Merged

core: ✨ add new package space-header #752

merged 7 commits into from
Jun 14, 2024

Conversation

enzostvs
Copy link
Member

Add the mini header into your project if running on a custom/sub domain

Screenshot 2024-06-13 at 11 24 02 AM

Example:

import { init } from "@huggingface/space-header"

// ...

init("enzostvs/lora-studio")

Must be called once the page has been mounted. If this is not the case, a log error is displayed to prevent the user

we've decided to stick with vanilla JS, which may evolve if necessary. (web component, etc)

@julien-c julien-c requested a review from jbilcke-hf June 13, 2024 15:42
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make this file shorter?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done!

@@ -0,0 +1,11 @@
export const inject_to_body = (
Copy link

@jbilcke jbilcke Jun 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perhaps we should add a class name or id, to make it easier for devs to change the position within the page?

that way they don't have to change their underlying app/page design in case the header gets in the way of something (toolbar etc)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hi @jbilcke nice idea!

i tried with both, class name or id, and I decided to go for the ID while className is probably not clear enough, tailwind class will not work etc.. so easier for us to target it through their own CSS file

thanks!

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh got it!

I missed the box.setAttribute("id", "huggingface-space-header");

all good 👍

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Related: I think we should allow users to provide a "target" and if so, rather than appending to body, append to that target.

Like with Svelte: (but same for any other framework)

https://svelte.dev/docs/client-side-component-api#creating-a-component

eg init(space, {target: "..."})

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@coyotte508 should be good now! tell me if you had something different in mind 🤗

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my mind it was more of a string that can be passed to document.querySelector, but a HTMLElement works too

@@ -0,0 +1,23 @@
import { inject_to_body} from "./inject_to_body"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we could add a prettifier to have unified spaces / semicolons, but that's just code-smetics

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code was on another project before (without prettier), so should be good now!

@enzostvs enzostvs requested a review from jbilcke June 13, 2024 16:22
@jbilcke
Copy link

jbilcke commented Jun 13, 2024

Thank you @enzostvs for doing this!

for some context I had started my own independent version based on Tailwind (my goal was to stay close to the original using Tailwind ie. save time in case the original design changes, let tailwind generate a tiny optimized stylesheet, let the framework generate a tiny static .js code injector etc - I would have put it into its own standalone module not just my ai stories factory app)

but I never finished as I got involved in other things, and I think you approach works fine too (this header shouldn't change that often)..

plus no dependencies which is great! 👌

@pngwn
Copy link
Member

pngwn commented Jun 14, 2024

Thanks for this PR, looks great, just have a few questions from a gradio PoV, where we may not know the user and space name up front.

  • we already make this request, and would need to make an extra one to get the space name anyway (you only know the space name when using this manually, we don’t have it on a subdomain), would it be possible to skip the API request and just pass in the Space object instead of a name?
  • the origin check isn’t sufficient for gradio as gradio apps can be hosted anywhere and all non Hugging Face urls will ‘pass’ the check causing the widget to render (colab/ locally/ etc). This isn’t anything that requires changes here but just mentioning it, we will handle this on the gradio side.

…he function to avoid the request to the HF API
@enzostvs
Copy link
Member Author

enzostvs commented Jun 14, 2024

Thanks for this PR, looks great, just have a few questions from a gradio PoV, where we may not know the user and space name up front.

  • we already make this request, and would need to make an extra one to get the space name anyway (you only know the space name when using this manually, we don’t have it on a subdomain), would it be possible to skip the API request and just pass in the Space object instead of a name?

@pngwn just pushed a change to cover your first point, now you can pass the namespace or directly the space information, here is the recommended info

export interface Space {
	id: string;
	likes: number;
	author: string;
}

@pngwn
Copy link
Member

pngwn commented Jun 14, 2024

Amazing. Thanks!

Copy link
Member

@pngwn pngwn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great from the gradio side! Thanks @enzostvs!

@julien-c
Copy link
Member

  • the origin check isn’t sufficient for gradio as gradio apps can be hosted anywhere and all non Hugging Face urls will ‘pass’ the check causing the widget to render (colab/ locally/ etc). This

Yes, you will use the Spaces env variables to check whether the Gradio app is running on Spaces, correct, @pngwn?

@pngwn
Copy link
Member

pngwn commented Jun 14, 2024

@julien-c yus! We will also hide when embedded via the web component I think as the space info is already in the wrapper.

One case we may not be able to figure out is if someone embeds using an iframe. I'm not sure what we want to do in that case, should we just show it?

@julien-c
Copy link
Member

One case we may not be able to figure out is if someone embeds using an iframe. I'm not sure what we want to do in that case, should we just show it?

you mean the gradio app runs on Spaces, but the raw URL like https://julien-c-coqui.hf.space is embedded in an iframe on a different website?

I would display it in that case yes (but no strong opinion)

@enzostvs enzostvs merged commit f925d99 into main Jun 14, 2024
5 checks passed
@enzostvs enzostvs deleted the feat/space-header branch June 14, 2024 13:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants