Comments (9)
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.
python does not provide any results.
from chaquopy.
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.
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.
what is the best way to get the value of python.stdout:
from chaquopy.
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.
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.
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.
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)
- OSError: Can't get source for <function swish_jit at 0x72d59118b0>. TorchScript requires source access in order to carry out compilation, make sure original .py files are available. HOT 2
- Could not resolve all artifacts for configuration ':classpath'. HOT 1
- Add support for Android Gradle plugin 8.6 HOT 1
- Remove support for buildPython version 3.8
- will u please to add duckdb,netcdf4,xarray packages? HOT 1
- Install of openai is failing with some issues related to jiter HOT 4
- Unable to install opencv-contrib-python HOT 1
- TomlError when building with pybase64 HOT 1
- jpype HOT 4
- Hope to support the following libraries HOT 7
- Compile Err when installing prophet HOT 2
- Failed to install pyzstd HOT 1
- Can't upgrade numpy library from 1.19 to 1.23.3 HOT 1
- Starting a web server causes the application to close itself HOT 1
- Build is not getting successful while installing pyramid-arima HOT 1
- SciPy for Python 3.11 or later
- [FR] com.chaquo.python should support com.android.dynamic-feature plugin HOT 2
- We need Ctranslate2 plz HOT 1
- Import the cantools library Failed HOT 2
- _ctypes not found HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from chaquopy.