GithubHelp home page GithubHelp logo

Comments (5)

Konubinix avatar Konubinix commented on August 11, 2024 1

example.zip

Here is a working example.

The content of the uploaded file is printed on the server part.

I think that this should work for small text files.

But if you need to send big files or binary files, you would rather want to show some upload progression, and the action/reaction mecanism seems not to be well suited for that.

from flexx.

almarklein avatar almarklein commented on August 11, 2024

There are two ways:

  • Create an action on the PyComponent. You can then call the action from the JS side.
  • Create a reaction on the PyComponents that listens to events on the JS side.

Also see https://flexx.readthedocs.io/en/stable/guide/patterns.html

from flexx.

korayoezdemir avatar korayoezdemir commented on August 11, 2024
class Upload(flx.PyComponent):
    CSS = "body {background-color: #F5F5DC;}"

    def init(self):
        self.input = Input()

    `@flx.action`
    def send(self, msg):
        print("send", msg)
class Input(flx.Widget):
    CSS = "body {background-color: #F5F5DC;}"

    def _create_dom(self):
        return flx.create_element("div")

    def _render_dom(self):
        return [
            flx.create_element(
                "input",
                {
                    "type": "file",
                    "onchange": lambda e: <callAnotherFunctionhere>(e.target.files[0]),
                },
            ),
        ]

these are my widgets, I want to upload a picture and then get the raw bytes of it and use it on my server side, how can i achieve this, when I call the send action with the onchange attribute, it does not work since it cant compile py to js, so far so good, you basically said what I have to do earlier, but I still cant figure out how to do this.

from flexx.

Konubinix avatar Konubinix commented on August 11, 2024

The initialization is a bit tricky as I use a class dedicated to emit events and I use a workaround to make sure things get loaded at the appropriate time with self.session.call_after_roundtrip(self.init_widgets)

There may be a simpler way to put things together, but at least, this example contains what you should need.

from flexx.

Konubinix avatar Konubinix commented on August 11, 2024

To upload big files, I would create a websocket handler in the tornado app, based on the implementation of flexx.app._tornadoserver.WSHandler and registered using flx.current_server().app.add_handlers("someurl", MYWSHandler) in the main before calling flx.start

That way, the client code could open the connection and send data while showing a nice upload progressbar.

I don't have time to write a simple example of this, so this is just an (hopefully useful) food for thoughts.

from flexx.

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.