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

WIP: add python console #144

Closed
wants to merge 6 commits into from

Conversation

tigercosmos
Copy link
Collaborator

No description provided.

m_pyconsole = console;
m_pyconsole->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);

m_pytext = new RPythonText(QString("Python"), this, Qt::WindowFlags(), console);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

share the console to RPythonText, since we need to update the output


void RPythonConsole::setConsole(std::string const & stdout_str, std::string const & stderr_str)
{
std::stringstream ss;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could be a temporary format
a more complete version can be in future work, such as red color for error.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use modmesh::Formatter for string formatting.

@@ -86,14 +95,19 @@ def get_appenv(name=None):
get_appenv(name='master')


def run_code(code):
def run_code(code, redirectStdOutFile=None, redirectStdErrFile=None):
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a possible improvement could be recording the history of the outputs.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not a right way to redirect buffered I/O in either Python or C++.

Add a dock widget at the bottom of the main window that a horizontally split text edit areas.  The upper and larger one is for displaying the history of the input Python commands.  The lower and narrow one is to input a Python command for execution.

The command line supports history navigation through up and down arrow keys on the keyboard, and uses enter for sending the command for execution.  It does not yet support multi-line Python code.

Improvements that are needed in the future:
* The output from Python goes to the process stdout and stderr that are not redirected to the Qt widget.  It should be in the future.
* The Python code dock widget on the right-hand side of the main window will be kept for a while before all the apps are migrated to use the new Python command console.
Copy link
Member

@yungyuc yungyuc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please take a look at #145.

R3DWidget * viewer() { return m_viewer; }

private:

void setUp();

RPythonText * m_pytext = nullptr;
std::shared_ptr<RPythonConsole> m_pyconsole = nullptr;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A very good reason is needed to justify the use of shared pointer with Qt objects, which has its own ownership system. Shared pointer does not work well with Qt pointers. I do not see why you need the shared pointer here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I use shared_ptr because the pointer is shared. I think a raw pointer is also fine.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should use signal/slot for accessing other Qt objects to make the event loop and ownership manager happy.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you give some references?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Googling the keywords in the sentence should be sufficient.

set_target_properties(
viewer
PROPERTIES
LINK_FLAGS_DEBUG "/SUBSYSTEM:CONSOLE" # open the console for debug mode
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it opens an additional debug console in release build, it needs to be changed.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in release mode, there is no cmd prompt.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To make it apparent, I recommend to only add the switch when it's not in release mode.


void RPythonConsole::setConsole(std::string const & stdout_str, std::string const & stderr_str)
{
std::stringstream ss;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use modmesh::Formatter for string formatting.

@@ -65,16 +69,50 @@ void RPythonText::runCode()
{
namespace py = pybind11;

const std::string redirect_stdout_file_path = "stdout.txt";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We may not redirect buffered I/O to files unconditionally like this.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is a proper way?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If by default it's redirected, there must be a runtime-configurable way to change it back. If it's undirected by default, a runtime-configurable way needs to be provided to turn on the redirection.

There may not be hard-coded file names to be written.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, in this case, it should be redirected, so there must be a configuration file?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it should be redirected, but there does not need to be a configuration file. Runtime configurability does not depend on configuration file.

@@ -160,6 +160,12 @@ class MODMESH_PYTHON_WRAPPER_VISIBILITY WrapRApplication
{
return self.main()->pytext();
})
.def_property_readonly(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Things get exposed from C++ to Python need corresponding unit tests. At the moment I do not see the need to access it from Python.

@@ -86,14 +95,19 @@ def get_appenv(name=None):
get_appenv(name='master')


def run_code(code):
def run_code(code, redirectStdOutFile=None, redirectStdErrFile=None):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not a right way to redirect buffered I/O in either Python or C++.

yungyuc and others added 4 commits November 9, 2022 22:37
Change all applications to set the Python command line instead.
Major changes include:

* The menubar initialization is moved from RApplication to RMainWindow.
* Remove redundant code in RAction.
* Main window title, resize, and show are exposed and moved to Python.
* Make modmesh.view.app an object (instead of function) again.
* Make sure RMainWindow::setUp() is only set up once.
* Populate app menu in RMainWindow::setUp().
* Keep RMainWindow::show() to Python.
@tigercosmos tigercosmos closed this Nov 9, 2022
@tigercosmos tigercosmos deleted the python-console branch November 9, 2022 17:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants