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

Can't add newline when using Highlighted Context #1839

Closed
dyxohjl666 opened this issue Jul 20, 2022 · 11 comments
Closed

Can't add newline when using Highlighted Context #1839

dyxohjl666 opened this issue Jul 20, 2022 · 11 comments
Assignees
Labels
bug Something isn't working
Milestone

Comments

@dyxohjl666
Copy link

The newlines don’t seem to work when I try to split the multiple results. I realized it maybe due to escaping but have no idea how to solve the problem.
What can I do for that? Or I have to switch to a html output?

@aliabid94 aliabid94 self-assigned this Jul 20, 2022
@aliabid94
Copy link
Collaborator

Thanks for letting us know about this. Will take a look!

@aliabid94
Copy link
Collaborator

For now the best way to handle this would be to affect the white-space css property of the HighlightedText component. In the example below, I've given the HighlightedText component the id 'htext', and then passed CSS to target the whitespace of the spans within the Component:

import gradio as gr

def test(text1):
    return [(text1, "good")]

demo = gr.Interface(
    test,
    "text",
    gr.HighlightedText(elem_id="htext"),
    css="#htext span {white-space: pre}",
)
demo.launch()

@aliabid94
Copy link
Collaborator

Let me know if this works for you, or if you have any questions!

@dyxohjl666
Copy link
Author

dyxohjl666 commented Jul 20, 2022

Thanks for your reply!
It looks quite perfect now when I apply a single Interface component.
But I met the same problem when adding the Interface into TabbedInterface. It seems like that the CSS is invalid in TabbedInterface. I'm confused about that.

@dyxohjl666
Copy link
Author

dyxohjl666 commented Jul 20, 2022

I generate the Interface like this:

import gradio as gr

parse_str = gr.Interface(
    fn=pred_str,
    inputs=gr.Textbox(
        label="String",
        lines=5,
    ),
    outputs=gr.HighlightedText(
        label="The Result of Parsing",
        combine_adjacent=True,
        adjacent_separator=" ",
    )
)

parse_file = gr.Interface(
    fn=pred_file,
    inputs="file",
    outputs=gr.HighlightedText(
        elem_id="htext",
        label="The Result of Parsing",
        combine_adjacent=True,
        adjacent_separator=" ",
    ),
    css="#htext span {white-space: pre-line}",
)
demo = gr.TabbedInterface([parse_str, parse_file], ["Parsing reference string", "Parsing reference for PDF"])

demo.launch(share=True)

@abidlabs abidlabs added the bug Something isn't working label Jul 20, 2022
@aliabid94
Copy link
Collaborator

Ahh hmm seems like a bug where custom CSS isn't passed up in a TabbedInterface. @abidlabs can you take a look?

@aliabid94 aliabid94 assigned abidlabs and unassigned aliabid94 Jul 20, 2022
@freddyaboulton
Copy link
Collaborator

This is a dupe of #1683. Going to keep both open so as to not lose all the discussion and add to 3.2 milestone.

@freddyaboulton freddyaboulton added this to the 3.2 milestone Aug 1, 2022
@abidlabs
Copy link
Member

abidlabs commented Aug 5, 2022

Hi @dyxohjl666, this is now fixed. See #1951 for more information, but the summary is that you will have to pass in a css parameter to TabbedInterface with your desired css, like this:

import gradio as gr

def test(text1):
    return [(text1, "good"), ("\n", None), (text1, "good")]

demo = gr.Interface(
    test,
    "text",
    gr.HighlightedText(elem_id="htext"),
)

def test(text1):
    return [(text1, "good"), ("\n", None), (text1, "good")]

demo2 = gr.Interface(
    test,
    "text",
    gr.HighlightedText(elem_id="htext2"),
)

gr.TabbedInterface([demo, demo2], css="#htext span {white-space: pre} #htext2 span {white-space: pre}").launch()

@dyxohjl666
Copy link
Author

It works well now ! Thanks a lot!

@emptystack1024

This comment was marked as resolved.

@emptystack1024
Copy link

你好,我的 gradio 版本是 4.32.0,当我运行上面的代码时,实现仍然与我的问题#8425相同,第二个选项卡上的操作仍然反馈到第一个选项卡,而第二个选项卡仍然没有得到任何输出。 2024-06-04 11-05-19 2024-06-04 11-05-19

solution:
See also: #8425
Thanks, @ Phillweston
The version that can be run is 4.31.5 version

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants