How can i display the pythoon scipt data within flask application [closed] - python

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
one: A.ipynb
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
i want when i click on the button the result will be appear in the application.
flask application
from A import factorial
app = Flask(__name__)
#app.route('/')
def hello():
#return factorial(5)
return "Hello"
if __name__ == '__main__':
app.run()```
how can i solve it

You should use str to return number data.
return str(factorial (5))

Related

How to get terminate or run code after a line that is blocking the code and does not pass command to next line? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 17 days ago.
Improve this question
I am trying to get the IP address and other details of Linux through my python code. when tmp = subprocess.call("./a.out") this line executes the code sticks here and this line subprocss.call("ifconfig") is not executed. as I cannot change the C code. so how can I make the next line run.
def execute():
dirname = '/home/kali/Downloads'
ext = ('.py','.c','.sh')
try:
for files in os.listdir(dirname):
if files.endswith(ext):
if files.endswith('.c'):
subprocess.call(["gcc", "code.c"])
tmp = subprocess.call("./a.out")
subprocss.call("ifconfig")
I got answer for this question after a long time of research. It can be done with a function subprocess.communicate function.
this function can communicate with the process.

Conditional - Try something for x seconds [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
This is my first question, as I have started to code recently.
here it goes...
I dont know how to do it, so I haven't coded it yet.
The idea is to have a Conditional to try doing something for x seconds, and if nothing happens on this x seconds, then do something to try it again.
Like this..
Try for 5 seconds to:
click on element to download something # <- this I know how to do
if nothing happens: # <- no error, just not executed the line above
refresh the page
try again:
finally:
You have your file
sorry for my English, as it is not my primary language and I am also learning it...
what web scraping tool you are using? selenium ? I can only give you my
logic if you do not post your code.
import datetime
def page_refresh():
print('refresh page')
driver.refresh()
def check_and_wait():
status_ready = False
while (not status_ready):
start_time = datetime.now()
while(not status_ready and datetime.now()-start_time<5): # loop 5 seconds
if(condtion == True): # if something happen
status_ready = True
return
page_refresh() # nothing happen , loop again

How to call function every 2 seconds in Python? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I am trying to figure out How to call function every 2 seconds in Python?
Should I use timer or thread?
Can anyone provide links/examples please?
The sched module can do that:
import sched, time
schedTimer = sched.scheduler(time.time, time.sleep)
def increaseX(x):
x += 1
print('X increased to ' + str(x))
schedTimer.enter(2, 1, increaseX, (x,))
schedTimer.enter(2, 1, increaseX, (3,))
schedTimer.run()

Why isn't this code executing? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
There are no syntax error or compilation error found. Why is not this program executing? I am not able to catch why is this program not running if has no compilation errors. What could logically go wrong in this below code?
What can I add into this code to make it run and interact?
def main():
print "Checking platform...\r\r\r"
platform = systemdetails()
def systemdetails():
print "Hello! Welcome to the auto-commander"
print "Please enter the platform specific number to automate."
platforminput = integer(input ("1. Cisco 2. Linux/Unix 3. Juniper 4. VMware vSphere/NSX \n:"))
if platforminput ==1:
platform='cisco_ios'
elif platforminput ==2:
platform='linux'
elif platforminput ==3:
platform='juniper'
elif platforminput ==4:
platform='vmware'
else:
print "Commander has to repeat the question...\n\n"
systemdetails()
return platform
You'll need to call your main function. A minimal example that still reproduces your problem is
def main():
print "Hello World!"
To get this to work, you need to call your main
def main():
print "Hello World!"
main()
Generally you only want to call your main if you are not being imported, which can be done like so
def main():
print "Hello World!"
if __name__ == '__main__':
main()
You need to call your main() function.
Either like this:
main()
Or like this:
if __name__ == "__main__":
main()
In the second example, main() will only be called if you run the program directly rather than importing the module and running functions individually. This way, you can import the module's functions to use separately without main() running when importing.

How can i improve this code to run faster? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I'm making The Powder Toy in Python, and I encountered some problems. Firstly the code runs VERY slowly. I know the problem is in my main file: http://pastebin.com/bbQ4H4Xu. The other files are just detecting input / creating the 2d array, so the problem isn't there.
Within my main file, the problem seems to be in the method updatescreen(). How can I increase the performance of this function?
import pygame
#inputkey.py
from pygame.locals import *
def input_key():
global inputt
inputt = ""
key = pygame.key.get_pressed()
if key[K_q]:
return 'q'
elif key[K_w]:
return 'w'
elif key[K_e]:
return 'e'
elif key[K_r]:
return 'r'
#Createblocks.py
blocks = []
for i in range(400):
blocks.append([])
for j in range(400):
blocks[i].append(0)
Your Main.py file has a print statement in the loop:
def updatescreen():
#The problem is here, it slows down the code.
for i in range(windh):
for x in range(windw):
print x, i # <== Here
if not blocks[i][x] == 0:
if blocks[i][x] == "Stone":
screen.blit(elementStone, (x,i))
presumably for debug? That's performing windw * windh = 2,500 print operations, which will slow the code down for sure. Try removing that and see how it improves.

Categories

Resources