Giter Site home page Giter Site logo

Infinite scroll example errors about nicegui HOT 5 CLOSED

psy0rz avatar psy0rz commented on June 2, 2024
Infinite scroll example errors

from nicegui.

Comments (5)

psy0rz avatar psy0rz commented on June 2, 2024 1

aweome thanks!! nicegui is the best :)

from nicegui.

falkoschindler avatar falkoschindler commented on June 2, 2024

Thanks for pointing this out, @psy0rz! We fixed it by explicitly handling the timeout.

from nicegui.

psy0rz avatar psy0rz commented on June 2, 2024

well now it works correctly, but on closer inspection: the timer runs on the server and send the javascript code to the client (browser), which in turn return the results.

this means that there will be a bunch of websocket messages 10 times per second, even if the user is not scrolling.

cant we do better? perhaps some javascript promise that gets resolved only when the user scrolls down enough to trigger additional content?

from nicegui.

falkoschindler avatar falkoschindler commented on June 2, 2024

Well, you're right. The current implementation might be a proof of concept, but not be very efficient.

Here's a rough idea how we could rewrite it using an intersection observer:

ui.add_head_html(r'''
<script>
document.addEventListener('DOMContentLoaded', (event) => {
    new IntersectionObserver(entries => emitEvent("bottom")).observe(document.querySelector('.footer'));
});
</script>
''')

def load_more():
    with images:
        for _ in range(10):
            ui.image(f'https://picsum.photos/640/360?{time.time()}').classes('w-32')

with ui.column() as images:
    load_more()
    ui.on('bottom', load_more)
ui.element().classes('footer')

I just don't like the initial call to load_more() and that we need to load multiple images so that the footer moves out of the viewport. Otherwise there won't be any intersection to observe. And we need a container for the image so that the footer remains below them.

I wonder if there's an easier way to achieve the same result...

from nicegui.

psy0rz avatar psy0rz commented on June 2, 2024

yes i suspected this would be one of those things that can be done, but will get complex very quickly :)

for now i'm fine with using a timer of 1 second for my application, but it would be nice if the example would be "perfect" in the future.

from nicegui.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.