Call a python function within a html file

You’ll need to use a web framework to route the requests to Python, as you can’t do that with just HTML. Flask is one simple framework: server.py: from flask import Flask, render_template app = Flask(__name__) @app.route(“https://stackoverflow.com/”) def index(): return render_template(‘template.html’) @app.route(“https://stackoverflow.com/my-link/”) def my_link(): print ‘I got clicked!’ return ‘Click.’ if __name__ == ‘__main__’: app.run(debug=True) templates/template.html: … Read more

Random numbers generation in PySpark

So the actual problem here is relatively simple. Each subprocess in Python inherits its state from its parent: len(set(sc.parallelize(range(4), 4).map(lambda _: random.getstate()).collect())) # 1 Since parent state has no reason to change in this particular scenario and workers have a limited lifespan, state of every child will be exactly the same on each run.

Python equivalent to perl -pe?

Yes, you can use Python from the command line. python -c <stuff> will run <stuff> as Python code. Example: python -c “import sys; print sys.path” There isn’t a direct equivalent to the -p option for Perl (the automatic input/output line-by-line processing), but that’s mostly because Python doesn’t use the same concept of $_ and whatnot … Read more

How to detect if a process is running using Python on Win and MAC

psutil is a cross-platform library that retrieves information about running processes and system utilization. import psutil pythons_psutil = [] for p in psutil.process_iter(): try: if p.name() == ‘python.exe’: pythons_psutil.append(p) except psutil.Error: pass >>> pythons_psutil [<psutil.Process(pid=16988, name=”python.exe”) at 25793424>] >>> print(*sorted(pythons_psutil[0].as_dict()), sep=’\n’) cmdline connections cpu_affinity cpu_percent cpu_times create_time cwd exe io_counters ionice memory_info memory_info_ex memory_maps memory_percent … Read more

pyodbc.connect() works, but not sqlalchemy.create_engine().connect()

A Pass through exact Pyodbc string works for me: import pandas as pd from sqlalchemy import create_engine from sqlalchemy.engine import URL connection_string = ( r”Driver=ODBC Driver 17 for SQL Server;” r”Server=(local)\SQLEXPRESS;” r”Database=myDb;” r”Trusted_Connection=yes;” ) connection_url = URL.create( “mssql+pyodbc”, query={“odbc_connect”: connection_string} ) engine = create_engine(connection_url) df = pd.DataFrame([(1, “foo”)], columns=[“id”, “txt”]) pd.to_sql(“test_table”, engine, if_exists=”replace”, index=False)

Python threading interrupt sleep

The correct approach is to use threading.Event. For example: import threading e = threading.Event() e.wait(timeout=100) # instead of time.sleep(100) In the other thread, you need to have access to e. You can interrupt the sleep by issuing: e.set() This will immediately interrupt the sleep. You can check the return value of e.wait to determine whether … Read more

Does Python GC deal with reference-cycles like this?

Python’s standard reference counting mechanism cannot free cycles, so the structure in your example would leak. The supplemental garbage collection facility, however, is enabled by default and should be able to free that structure, if none of its components are reachable from the outside anymore and they do not have __del__() methods. If they do, … Read more

Python: Converting GIF frames to PNG

I don’t think you’re doing anything wrong. See a similar issue here: animated GIF problem. It appears as if the palette information isn’t correctly treated for later frames. The following works for me: def iter_frames(im): try: i= 0 while 1: im.seek(i) imframe = im.copy() if i == 0: palette = imframe.getpalette() else: imframe.putpalette(palette) yield imframe … Read more

PyTorch Binary Classification – same network structure, ‘simpler’ data, but worse performance?

TL;DR Your input data is not normalized. use x_data = (x_data – x_data.mean()) / x_data.std() increase the learning rate optimizer = torch.optim.Adam(model.parameters(), lr=0.01) You’ll get convergence in only 1000 iterations. More details The key difference between the two examples you have is that the data x in the first example is centered around (0, 0) … Read more