-1

How can the iPython console in Spyder be cleared with code in the console? I'm looking for something I could use in a simple text game.

This method doesn't work:

import os
clear = lambda: os.system("cls")
clear()

This command does clear the console,

print("\033[H\033[J", end="")

but it causes issues with other statements in the same console input. For instance,

print("\033[H\033[J", end="")
print("Hello world!")

doesn't print Hello world.

DoodleVib
  • 159
  • 13
PLrc
  • 19
  • 3
  • Your code works fine using `ipython` on windows, it clears the console and prints "Hello world". In the Cosnole in spyder the command clear() also works out of the box, so I see no problem there? What is your problem exactly? – 3dSpatialUser Sep 16 '21 at 12:41

1 Answers1

0

If you're using the Spyder console, note that the Spyder console is an implementation of iPython. To clear the displayed text try:

from IPython import get_ipython
get_ipython().magic('clear')

There is additional information available on this post.

For your text game, you should only rely on this approach if the end product is going to be used in an iPython console (like the Spyder console).

DoodleVib
  • 159
  • 13