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

Add an interactive Python command console #145

Merged
merged 1 commit into from
Nov 5, 2022

Conversation

yungyuc
Copy link
Member

@yungyuc yungyuc commented Nov 5, 2022

For #104

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.

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.
@yungyuc yungyuc added the enhancement New feature or request label Nov 5, 2022
@yungyuc yungyuc self-assigned this Nov 5, 2022
Copy link
Member Author

@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.

@tigercosmos It happens that I am working on a feature similar to your #144. But in #144 there is not history navigation. I think we can take both PRs after reviewing both.

@@ -40,6 +40,10 @@ void RMainWindow::setUp()
m_pytext->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
addDockWidget(Qt::RightDockWidgetArea, m_pytext);

m_pycon = new RPythonConsoleDockWidget(QString("Console"), this);
m_pycon->setAllowedAreas(Qt::BottomDockWidgetArea | Qt::TopDockWidgetArea);
Copy link
Member Author

Choose a reason for hiding this comment

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

Allow the console to be at the bottom or the top. Having it in the left or right could make it too narrow.

interp.exec_code(code);
}

void RPythonConsoleDockWidget::navigateCommand(int offset)
Copy link
Member Author

Choose a reason for hiding this comment

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

Navigate through the history. Slight complexity.

m_command_string = m_command_edit->toPlainText().toStdString();
}

int new_index = m_current_command_index + offset;
Copy link
Member Author

Choose a reason for hiding this comment

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

Limit the range of new_index.

new_index = 0;
}

if ((0 == m_past_command_strings.size()) || (new_index == m_current_command_index))
Copy link
Member Author

Choose a reason for hiding this comment

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

If there's nothing in the history or the new index does not change, do nothing.

}
// Move cursor to the end of line.
{
QTextCursor cursor = m_command_edit->textCursor();
Copy link
Member Author

Choose a reason for hiding this comment

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

textCursor() returns a copy.

:ivar locals:
The local namespace of the application.
"""
def __init__(self, name):
self.globals = {}
self.locals = {
self.globals = {
Copy link
Member Author

Choose a reason for hiding this comment

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

Change to have the symbols to appear in the "module global".

@yungyuc
Copy link
Member Author

yungyuc commented Nov 5, 2022

@tigercosmos I am checking it in for discussions in the sprint later this afternoon. We can still discuss in this PR.

@yungyuc yungyuc merged commit 5680177 into solvcon:master Nov 5, 2022
@yungyuc yungyuc deleted the feature/python-console branch November 5, 2022 03:29
def exec_code(code):
try:
apputil.run_code(code)
except Exception as e:
Copy link
Collaborator

Choose a reason for hiding this comment

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

this doesn't work well I think.
for example, I enter print(a) (with undefined a) in the console, it will just crash.

@tigercosmos
Copy link
Collaborator

@yyc so based on this change, I will modify #144 as the followings:

  • RPythonText's stdout and stderr will go to this new RPythonConsoleDockWidget.
  • RPythonConsoleDockWidget's stdout and stderr will just show right after the command.

@yungyuc
Copy link
Member Author

yungyuc commented Nov 5, 2022

You tagged another one but not me. Hope he/she does not mind :)

  • RPythonText's stdout and stderr will go to this new RPythonConsoleDockWidget.

Yes. But in general, the buffered output of the Python interpreter should be redirected. We do not need to limit it to the RPythonText, which will be removed later.

  • RPythonConsoleDockWidget's stdout and stderr will just show right after the command.

And we should find an apparent way to distinguish the command from the output.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants