Forums

How to run the py program in CLI

I am new to this site. I want to run the py program in CLI automatically when I pull up the file. So i want to show the CLI interface and program run in that interface for input and output. In short I just want to duplicate what I do in command line interface online just by clicking a link to my *.py file.

Thanks

I'm not sure I understand what you want, but I think you can do it by adding a custom console (which is something we actually only released today :-)

Go to the "Consoles" tab, and click on the small "+" next to "Custom". This will display fields where you can enter a name for the custom console, and the command to run. For the name, enter anything you want. For the command, enter the bash command to run the script, eg. python3.4 /home/hemantjha/my_script.py. Click on the blue checkmark button to add the custom console.

Now whenever you go to the "Consoles" tab, you can click on the custom console name, and your script will run.

What I am looking is a webpage where I click on the link so that it opens up command line interface with my script running so that user can interact directly in the CLI environment. I tried your suggestion it didn't work. possible I missed some steps.

So you want your users, people on the Internet, to be able to interact with your program via a CLI?

I wonder if you could use a "gist console" to do what you want? Here is an example: https://www.pythonanywhere.com/gists/5502596/robot.py/ipython3/

To create your own, you would need to:

  • go to gist.github.com
  • create a python script, and save it (try it with a simple print("hello world") first)
  • then, go to https://www.pythonanywhere.com/gists/, and paste in the URL to your gist
  • then you'll get a link to a "gist console", which you can give to anyone on the internet. When they click it, they'll get a CLI with the python code from your gist in it...

Thanks for the solution it works. But it is not and elegant solution. I got drawn to pythonanywhere looking at the implementation of IDLE on www.python.org. The IDE is built in and elegant. I want to invoke the IDE with argument say my program hello.py and it should just run the program interactively. The gist.github implementation doesn't look seamless.

Your help will be appreciated.

In short I want to start up the Python Console (as already implemented at pythonanywhere) with a script running say hello world. I think that should easy since it already there. I just need the syntax so that I can click on a link on my webpage and it starts the console with script running.

Can you give me a link to your gist console? It might give me a better idea of what it is you're trying to do...

OK i figured out what I wanted. Here is the example. But how do I close the console. I get an error when I do exit() at the end. https://www.pythonanywhere.com/user/hemantjha/files/home/hemantjha/Krish.py?run

Is it sys.exit() that you want?

Once you've got your script working the way you want it to, try copy-pasting it into a gist on gist.github.com, and then creating a gist console for it on pythonanywhere.com/gists/