Giter VIP home page Giter VIP logo

Comments (9)

mhsmith avatar mhsmith commented on September 16, 2024

As I've said before, I can only help you if you explain what you mean by "does not work". What exactly happened?

from chaquopy.

claudiosx avatar claudiosx commented on September 16, 2024

python does not provide any results.

from chaquopy.

mhsmith avatar mhsmith commented on September 16, 2024

In which Java method have you put this code?

It might save time if you put the app's source code on GitHub so I can see the context, or at least a simplified version which shows the same problem.

from chaquopy.

claudiosx avatar claudiosx commented on September 16, 2024

the "valutazione" function is posed near CreateOutput, takes a string from the text field formula and passes it to etInput adding a "\n"

 public String valutazione(){
        valuta value;
        String ValIN = "";
        String Risp="";

        tvOutput.setText("");
        ValIN = PreValutazione(this.formula.getText().toString());
        ValIN=ValIN+'\n';

        this.formula.setText("");
        this.formula.setText(ValIN);

        etInput.setTextColor(Color.BLACK);
        etInput.setText(ValIN);
        etInput.setTextColor(Color.WHITE);

       

        Risp=tvOutput.getText().toString() ;

        while (true) {
            Risp = tvOutput.getText().toString();
            if(Risp.toLowerCase().contains("sci_>")) break;
        }
/*
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
      */

        Risp=tvOutput.getText().toString() ;

        this.formula.setText("");
        formula.setTextColor(Color.WHITE);
        Risp.replace("SCi_>", "");

        formula.setText(">"+Risp );
        return Risp;
    }
    private void createOutput() {
        svOutput = findViewById(resId("id", "svOutput"));
        svOutput.getViewTreeObserver().addOnGlobalLayoutListener(this);

        tvOutput = findViewById(resId("id", "tvOutput"));
        if (Build.VERSION.SDK_INT >= 23) {
            tvOutput.setBreakStrategy(Layout.BREAK_STRATEGY_SIMPLE);
        }
        // Don't start observing task.output yet: we need to restore the scroll position first so
        // we maintain the scrolled-to-bottom state.
    }

    @Override protected void onRestoreInstanceState(Bundle savedInstanceState) {
        // Don't restore the UI state unless we have the non-UI state as well.
        if (task.getState() != Thread.State.NEW) {
            super.onRestoreInstanceState(savedInstanceState);
        }
    }

from chaquopy.

claudiosx avatar claudiosx commented on September 16, 2024

what is the best way to get the value of python.stdout:

from chaquopy.

mhsmith avatar mhsmith commented on September 16, 2024

I still can't tell what context the code is being called in. The console example app contains two threads:

  • The main thread, which handles the Android UI. If you run a while (true) loop in this thread, you'll lock up the entire app and probably get a "not responding" dialog.
  • The background thread, created in ConsoleActivity.Task.start(). If you run a while (true) loop in this thread, then nothing else will never get a chance to happen in this thread, including Python code.

If you have a Python script running in the background thread, and you want to intercept its output in Java, then you should look at overriding either ConsoleActivity.output() (which runs on the main thread) or ConsoleActivity.Task.output() (which runs on the background thread).

Or if you want to run some code repeatedly on the main thread without blocking it, try using Handler.postDelayed to schedule a callback. Then every time it calls you back, you can call it again to schedule the next call.

Finally, I'm not certain that setting a string containing "\n" in the input box will actually cause an input event. It might be better to call PythonConsoleActivity.Task.onInput() directly.

from chaquopy.

claudiosx avatar claudiosx commented on September 16, 2024

my metod valutazione () is member of ConsoleActivity()
in this metod , for getting value from formula textbox .
I modified the valutazione code as shown below:
ValIN = this.formula.getText().toString();
ValIN=ValIN+'\n';
task.onInput(ValIN);
and this code work fine.

in valutazione() metod
I tried to get result of python whit this code:

 PyOUT.setText("");

  while (true) {
      Risp=PyOUT.getText().toString();
        if (Risp.contains("END")) break;
}

the string END is inserted by python at the end of each processing

I have some difficuluties to getting result data from output.
I modified output below shown:

private void output(CharSequence text) {

 String mypy="";

    removeCursor();
    if (consoleModel.pendingNewline) {

        tvOutput.append("\n");
        consoleModel.pendingNewline = false;
    }
    if (text.charAt(text.length() - 1) == '\n') {

        tvOutput.append(text.subSequence(0, text.length() - 1 ));
        consoleModel.pendingNewline = true;

    } else {
        mypy=(String) text;
        PyOutput=tvOutput.getText().toString();
        PyOUT.setText(mypy);
        debug(mypy,false);
        tvOutput.append(mypy);
    }



    // Changes to the TextView height won't be reflected by getHeight until after the
    // next layout pass, so isScrolledToBottom is safe here.
    if (isScrolledToBottom()) {
        scrollTo(Scroll.BOTTOM);
    }

}

t but it does not exit the evaluation cycle in valutazione() metod , and sometimes when it comes out it does not return the processing values but only the string "END".

there is no method for taking the various values of python output and choosing which one you want

from chaquopy.

mhsmith avatar mhsmith commented on September 16, 2024

I still can't tell which thread that while (true) loop is running on, but as I explained, neither of the existing threads are suitable. And if you're overriding the output() method, there's no need to repeatedly check the output anyway. All the output goes through that method, so you can invoke all your processing from there.

It's difficult to understand these out of context fragments. I could help you more easily if you create a GitHub fork of the example app you used, and upload your changes, or at least a simplified version which demonstrates the problem.

from chaquopy.

mhsmith avatar mhsmith commented on September 16, 2024

sometimes when it comes out it does not return the processing values but only the string "END"

The output() function receives the text in fragments, usually one line at a time. Then you're setting each fragment to PyOUT (which I assume is another TextView). Each call to setText overwrites the previous text. So of course when you come to read PyOUT, only the most recent fragment will be there.

from chaquopy.

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.